与普通JavaScript中的jQuery代码等效
我试图在JavaScript中使用以下代码行,但它们在jQuery中:与普通JavaScript中的jQuery代码等效,javascript,jquery,Javascript,Jquery,我试图在JavaScript中使用以下代码行,但它们在jQuery中: $(document).on('focusin', function(e) { if ($(e.target).closest(".mce-window").length) { e.stopImmediatePropagation(); } }); 如何在普通JavaScript中实现上述功能?将在除IE之外的mos浏览器上运行(尽管Edge支持)。对于IE,您可以如下定义原型: if (!Element.
$(document).on('focusin', function(e) {
if ($(e.target).closest(".mce-window").length) {
e.stopImmediatePropagation();
}
});
如何在普通JavaScript中实现上述功能?
将在除IE之外的mos浏览器上运行(尽管Edge支持)。对于IE,您可以如下定义原型:
if (!Element.prototype.matches)
Element.prototype.matches = Element.prototype.msMatchesSelector;
if (!Element.prototype.closest) {
Element.prototype.closest = function (selector) {
var el = this;
while (el) {
if (el.matches(selector)) {
return el;
}
el = el.parentElement;
}
};
}
现在,您可以使用以下代码:
document.addEventListener('focus',function(e){
if ((e.target).closest("tbody").length) {
e.stopImmediatePropagation();
}
}, true);
是一个很好的起点。Nestest是在vanilla JS中发布的,但不受跨浏览器的广泛支持。解决方法是将js内联放置在HTML元素中。这很好用。@RonRoyston它实际上有很好的支持,只要你不需要IE11()