jquery multiple toggle on IE

别来无恙 提交于 2020-01-06 06:42:10

问题


im facing issues with multiple toggling on IE. it works fine with all other browser except IE (-_-). below is a abstract of my code.basically im only able to get an popup alert when i clicked on the first hyperlink. there were no popup when i clicked on subsequent links

 $(document).ready(function(){

$("a#toggleFruitSlideBox").click(function() {
          alert($(this).text());
      return false;
  });
});

<div id="bodykit_slidebox">
<div style="padding:5px 0px 0px 5px;">
    <a id="toggleFruitSlideBox" href="#" class="nav2">apple</a>
    <a id="toggleFruitSlideBox" href="#" class="nav2">orange</a>
    <a id="toggleFruitSlideBox" href="#" class="nav2">DURIAN</a>
    <a id="toggleFruitSlideBox" href="#" class="nav2">papaya</a>
</div>

回答1:


You have multiple elements with the same id it is an invalid HTML!

Check my answer here:
jQuery id selector works only for the first element

The only difference between your code and the code in the other question is that you use a bad selector:

$("a#toggleFruitSlideBox")

Which cause jQuery not to use the document.getElementById, so this is why it works in other browsers.

From the jQuery docs:

For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.

Remove the duplicated id and use other selector like the class selector.




回答2:


Looks like the problem is that all your Id's are the same. Id's are supposed to be unique. Either use a class or just use the a selector if you don't care if it fires on all a tags.



来源:https://stackoverflow.com/questions/12926735/jquery-multiple-toggle-on-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!