我刚刚进入jQuery,我在理解它是什么时遇到了问题.如何在jQuery对象上使用数组样式索引但jQuery不是数组?这是一个
javascript的东西吗?
<ul id="myUL">
<li></li>
<li id="second"></li>
<li></li>
</ul>
var jqueryObject = $("#myUL > li");
alert(jqueryObject[1].attributes.getNamedItem("id").value);
if (jqueryObject instanceof Array) {
alert('value is Array!');
} else {
alert('Not an array');//this is what pops up
}
解决方法
jQuery集合是一个Object,其属性编号为Array索引(以及一些其他属性和方法),每个属性都包含一个匹配的元素.它还有一个length属性来告诉你选择器匹配了多少元素.见
http://api.jquery.com/Types/#jQuery
此外,是的,它部分是JavaScript的东西 – JS允许您使用点表示法或方括号表示法访问对象的属性.