javascriptsizzle

how to use sizzle.js separate


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

<!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 alert "[object]", "undefined", please tell me what's wrong with my code?


Solution

  • The Sizzle() function returns an array of matched elements. So if you know there'll be exactly one matching element (which there should be if you're selecting by id) try:

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