How to use sizzle.js separately

I downloaded sizzle.js from https://github.com/jquery/sizzle my code:

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="sizzle.js" type="text/javascript"></script>
    <script type="text/javascript">
        window.onload=load;
        function load(){
            alert(Sizzle("#test"));
            alert(Sizzle("#test").innerHTML);
        }
    </script>
</head>
<body>
<div id="test">abc</div>
</body>
</html>

but warn "[object]", "undefined", please tell me what happened to my code?

+5
source share
2 answers

The function Sizzle()returns an array of matched elements. Therefore, if you know that there will be exactly one corresponding element (which should be if you select by id), try:

alert(Sizzle("#test")[0].innerHTML); 
+6
source

You made a small mistake, it returns more than one . similar to an array, but used for storage . You might want to use the first one. NodeList Node NodeList Node

// this is how you do it
alert( Sizzle('#test')[0].innerHTML );
0
source

All Articles