我正在尝试使用 jQuery 的 has
我的其中一个菜单中的方法来检查当前是否为 <li>
有任何 <ul>
在里面。如果是,则显示它们,否则隐藏 li
.
但是当使用 has
时我收到此错误:
Uncaught TypeError: Object # has no method 'has'
我的代码:
$('nav ul li').click(function () {
console.log(this.has('ul')); //Checking
if ($(this).children('ul').is(":visible")) {
$(this).children('ul').slideUp(250);
} else {
$(this).children('ul').slideDown(250);
}
});
最佳答案
你稍后执行 $(this)
的原因是因为 this
不是 jQuery 对象。您仍然需要执行 $(this).has('ul')
。
请注意,您还可以使用 $(this).children('ul').slideToggle(250);
来简化切换部分。
关于javascript - 未捕获的类型错误 : Object #<HTMLLIElement> has no method 'has' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19339460/