简体   繁体   English

jQuery的最接近在IE8 / 9中不起作用

[英]jQuery's closest doesn't work in IE8/9

I have this jQuery code: 我有这个jQuery代码:

$(this).closest('div:has(.FIND_ME)').find('.FIND_ME').hide();

But element with class .FIND_ME doesn't hide in IE8 and 9. 但是具有类.FIND_ME元素不会隐藏在IE8和9中。

This question is a continuation of Search for an item with a common ancestor 此问题是搜索具有共同祖先的项目的延续

HTML: HTML:

<div>
    <div><!-- all div without ID -->
        <span>some text</span>
        <div>
          <span id="listener1">click here</span>
          <span>sometext</span></div>
        <div>

        <span class="FIND_ME">Result Here</span></div>
    </div>

    <div>
        <span>some text</span>
        <div id="div1">
         <div id="div2">
          <span id="listener2">click here</span>
          <span>sometext</span></div>
         </div>
        <div>

        <span class="FIND_ME">Result Here</span></div>
    </div>
</div>

I was setting a variable element to this , then later on I was calling: 我是一个变量设置elementthis ,再后来我打电话:

element.closest('a')

But element was now the DOM element instead of the jQuery object. 但是element现在是DOM元素而不是jQuery对象。 So changing to: 所以改为:

$(element).closest('a')

fixed it. 修复。

You're right! 你是对的! I don't know why, but it works now! 我不知道为什么,但它现在有效! The mistake was in an another place. 错误发生在另一个地方。

Thus, closest() works fine in IE 8/9. 因此, closest()在IE 8/9中运行良好。 Tested on jQuery 1.6. 在jQuery 1.6上测试过。

closest = function (target, tag) {
    if (target.parentElement == "undefined") {
        return null;
    }
    if (target.parentElement.localName == tag) {
        return target.parentElement;
    }
    return this.closest(target.parentElement, tag);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM