javascript jQuery hasClass“不是函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30630800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery hasClass 'is not a function'
提问by rubencart
I want to check with jQuery which li element from a list of li elements is the active one (they represent a menu, and I want to check which menu item is currently active).
我想用 jQuery 检查 li 元素列表中的哪个 li 元素是活动的(它们代表一个菜单,我想检查哪个菜单项当前处于活动状态)。
So, I store all the li items in a variable, myElements. Then I ask the length of myElements, and apply some style changes to them, up to here everything works. Apparently this way my variable myElements is a Nodelist, not an array, so I declare another variable; myElements_array, which contains the same elements as myElements and is an array (also tested and it works).
因此,我将所有 li 项存储在变量 myElements 中。然后我询问 myElements 的长度,并对它们应用一些样式更改,到这里一切正常。显然这样我的变量 myElements 是一个节点列表,而不是一个数组,所以我声明了另一个变量;myElements_array,它包含与 myElements 相同的元素并且是一个数组(也经过测试并且可以工作)。
Then I try to check which of the elements from myElements_array has the 'current-menu-item' class, but it doesn't work and the google chrome console says there's an error: 'Uncaught TypeError: myElements_array[j].hasClass is not a function'. Does anyone have an idea what the reason might be?
然后我尝试检查 myElements_array 中的哪些元素具有 'current-menu-item' 类,但它不起作用,谷歌浏览器控制台说有一个错误:'Uncaught TypeError: myElements_array[j].hasClass is not一个函数'。有谁知道原因可能是什么?
<script type='text/javascript'>
var myElements = jQuery("#navbar > ul > li");
var count = myElements.length;
for (var i = 0; i < myElements.length; i++) {
myElements[i].style.width = (100/count)+'%';
}
var myElements_array = [];
for(var i = myElements.length; i--; myElements_array.unshift(myElements[i]));
var j = 0;
while (! myElements_array[j].hasClass('current-menu-parent') ) {
j++;
}
document.write(j);
</script>
回答by epascarello
Problem is the index you are pulling from the array is a DOM node when you use bracket notation and it is not a jQuery object. DOM does not have hasClass.
问题是当您使用括号表示法时,您从数组中提取的索引是一个 DOM 节点,而不是一个 jQuery 对象。DOM 没有 hasClass。
You can either store the jQuery version or change it to jQuery
您可以存储 jQuery 版本或将其更改为 jQuery
while (! $(myElements_array[j]).hasClass('current-menu-parent') ) {
or use classList contains
或使用 classList 包含
while (! myElements_array[j].classList.contains('current-menu-parent') ) {
or use eq()
instead of referencing the DOM
或使用eq()
而不是引用 DOM
while (! myElements_array.eq(j).hasClass('current-menu-parent') ) {
回答by Barmar
When you access a jQuery object as if it's an array, it returns the raw DOM element object, not a jQuery object. If you want a jQuery object, use .eq()
rather than an array index:
当您像访问数组一样访问 jQuery 对象时,它返回原始 DOM 元素对象,而不是 jQuery 对象。如果你想要一个 jQuery 对象,请使用.eq()
而不是数组索引:
while (myElements.eq(j).hasClass('current-menu-parent') ) {
j++;
}
You could also use:
您还可以使用:
j = myElements.index(myElements.find(".current-menu-parent:first"));
回答by Okku
You shouldn't loop through elements inside an jQuery object like that. You're tyring to use jQuery methods on normal dom objects. Use this instead:
你不应该像那样循环遍历 jQuery 对象中的元素。您很想在普通的 dom 对象上使用 jQuery 方法。改用这个:
$("#navbar > ul > li").each(function(){
$(this).hasClass("current-menu-parent");
});