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 badges2 Answers
Reset to default 6The 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 Node
s. You may probably want to use the first one.
// this is how you do it
alert( Sizzle('#test')[0].innerHTML );