在纯JavaScript中相当于jQuery代码

时间:2017-12-01 04:37:38

标签: javascript jquery

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

$(document).on('focusin', function(e) {
  if ($(e.target).closest(".mce-window").length) {
    e.stopImmediatePropagation();
  }
});

如何在Vanilla JavaScript中执行上述操作?

1 个答案:

答案 0 :(得分:0)

closest适用于除IE之外的其他浏览器(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);