最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to use sizzle.js separate - Stack Overflow

programmeradmin1浏览0评论

I downloaded sizzle.js from 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?

I downloaded sizzle.js from https://github./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?

Share Improve this question edited Feb 20, 2012 at 3:38 Timothy Jones 22.2k6 gold badges64 silver badges95 bronze badges asked Feb 20, 2012 at 3:11 artwlartwl 3,5827 gold badges41 silver badges55 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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); 

You've did a slight mistake it returns NodeList not a single Node. The NodeList is like an array but used for storing Nodes. You may probably want to use the first one.

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

评论列表(0)

  1. 暂无评论