我遇到了 Jquery items[c] 被明确定义为 HTMLElement 的问题,但是当我将 .find 与多个选择器一起使用时,我收到此错误:
TypeError: X[g].exec is not a function
http://code.jquery.com/jquery-latest.min.js
当我检查一些手表时,我得到了下图中的内容:
$(items[c]).find('.received')
工作正常并返回一些元素,因为存在该类的元素
$(items[c]).find('.receive')
也可以正常工作并返回零元素,因为没有该类的元素。
但是
$(items[c]).find('.received.unseen')
返回未定义和错误。那么这里发生了什么?
编辑:这是我遇到错误的函数,我切换到jquery 2.1.1:
function updateUnseenBell(){
var m;
for (var c in items)
if (items.hasOwnProperty(c) && (m = items[c].gEbCN("chat-partner-tab")[0])) {
if($(items[c]).find('.received.unseen:not(.shown)').length > 0){
if (!(m.l2_newMsgBell)) {
m.appendChild(m.l2_newMsgBell = newMsgBell.cloneNode());
playSound("message");
}
} else if (m.l2_newMsgBell) {
m.removeChild(m.l2_newMsgBell);
delete m.l2_newMsgBell;
}
}
}
我将其减少到最低限度以进行调试,但仍然遇到相同的错误:
function updateUnseenBell(){
for (var c in items) {
if (items.hasOwnProperty(c)) {
if ($(items[c]).find('.received.unseen:not(.shown)').length > 0) {
alert(1);
} else {
alert(2);
}
}
}
}
使用
$(items[c]).find('.message.received.unseen')
这应该可行。
解决此问题的另一种方法是
$(items[c]).find(".received").find(".unseen").find(":not(.shown)")
这不是一个优雅的方法,但也很有效。
您可以使用的另一种方法:
$(items[c]).find(".received, .unseen, :not(.shown)");