原因IE仍然不支持element.closest()
最简单的做法是使用polyfill。
包括this js(这是填充工具对最近())
(function (ElementProto) {
\t if (typeof ElementProto.matches !== 'function') {
\t \t ElementProto.matches = ElementProto.msMatchesSelector || ElementProto.mozMatchesSelector || ElementProto.webkitMatchesSelector || function matches(selector) {
\t \t \t var element = this;
\t \t \t var elements = (element.document || element.ownerDocument).querySelectorAll(selector);
\t \t \t var index = 0;
\t \t \t while (elements[index] && elements[index] !== element) {
\t \t \t \t ++index;
\t \t \t }
\t \t \t return Boolean(elements[index]);
\t \t };
\t }
\t if (typeof ElementProto.closest !== 'function') {
\t \t ElementProto.closest = function closest(selector) {
\t \t \t var element = this;
\t \t \t while (element && element.nodeType === 1) {
\t \t \t \t if (element.matches(selector)) {
\t \t \t \t \t return element;
\t \t \t \t }
\t \t \t \t element = element.parentNode;
\t \t \t }
\t \t \t return null;
\t \t };
\t }
})(window.Element.prototype);
,然后它就像IE浏览器可以理解它,它的工作原理与所有没有其他变化。
from jonathantneal/closest
['。家长()'](http://api.jquery.com/parents/)? – 2015-02-09 09:29:07
http://stackoverflow.com/questions/15329167/closest-ancestor-matching-selector-using-native-dom – BeNdErR 2015-02-09 09:29:23
嗨,应该说,.parent()不会在这个实例中工作,因为一些页面, img在它周围封装了一个href标签,它将删除这个元素而不是td。所以它真的需要找到最接近的td – 2015-02-09 09:31:04