最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery's closest doesn't work in IE89 - Stack Overflow

programmeradmin1浏览0评论

I have this jQuery code:

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

But element with class .FIND_ME doesn't hide in IE8 and 9.

This question is a continuation of Search for an item with a common ancestor

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 have this jQuery code:

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

But element with class .FIND_ME doesn't hide in IE8 and 9.

This question is a continuation of Search for an item with a common ancestor

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>
Share Improve this question edited Nov 9, 2017 at 2:38 Undo 25.7k38 gold badges112 silver badges131 bronze badges asked Jan 20, 2015 at 8:52 DmitMedvDmitMedv 1,0103 gold badges13 silver badges22 bronze badges 9
  • 4 And it works in all other browsers ? – adeneo Commented Jan 20, 2015 at 8:52
  • 1 I can guarantee you that closest() works fine in IE8/9. The issue will lie in your code somewhere. Please add your relevant HTML to the question. – Rory McCrossan Commented Jan 20, 2015 at 8:53
  • I seriously doubt such a "simple" thing would not be cross-browser... Here's a tip : 99% of the time, the issue is not jQuery but the way you use it, always try to figure out what you did wrong instead of blaming jQuery, you'll end up sparing a lot of time. – Laurent S. Commented Jan 20, 2015 at 8:55
  • 1 It works fine in a fiddle: jsfiddle.net/RoryMcCrossan/h34L271a Check the console for errors elsewhere in your code. – Rory McCrossan Commented Jan 20, 2015 at 8:56
  • 1 try as $(this).parent("div").next("span.FIND_ME").hide(); – Sanjay Commented Jan 20, 2015 at 8:59
 |  Show 4 more comments

3 Answers 3

Reset to default 15

I was setting a variable element to this, then later on I was calling:

element.closest('a')

But element was now the DOM element instead of the jQuery object. So changing to:

$(element).closest('a')

fixed it.

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

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. Tested on jQuery 1.6.

发布评论

评论列表(0)

  1. 暂无评论