Vanilla JavaScript .closest without jQuery

前端 未结 3 786
庸人自扰
庸人自扰 2020-12-07 00:34

I\'m working on an application that only has jQuery 1.1 installed which dosn\'t support the .closest method.

My script currently looks like this:

$(\         


        
3条回答
  •  执念已碎
    2020-12-07 00:56

    Modern browsers have the Element.closest() method.

    Example:

    document.querySelector('.navPanel img').closest('td')
    

    Reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/closest

    Here you can check which browsers have built-in support: https://caniuse.com/#feat=element-closest

    When there is no support in the browser the following polyfill could be used: https://github.com/jonathantneal/closest

提交回复
热议问题