Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与普通JavaScript中的jQuery代码等效_Javascript_Jquery - Fatal编程技术网

与普通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.

我试图在JavaScript中使用以下代码行,但它们在jQuery中:

$(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()