原因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