我想了解是否支持 closest () 方法,如果需要(IE 11),请使用以下形式的替代方法:
ancestor = ('closest' in document.documentElement) ? element.closest (selector) : element.parentElement;
我的浏览器(Edge、Chrome、Firefox、IE 11 - WIN 10)的控制台按预期返回“true”或“false”。
这个解决方案是干净的解决方案还是有任何我没有考虑到的陷阱?
最佳答案
最好使用 polyfill 检查方法是否存在,如果不存在,则添加一个等效于对象原型(prototype)的方法。然后在所有浏览器中使用该方法。
https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
关于javascript - 检测是否支持方法closest(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57547432/