0
<div class="subitems">
  <ul>
      <li id="China"></li> 
         <li id="India">
       India
       <div id="subitems">
         <ul>
            <li id="India/Sub Item 1">Sub Item 1</li>
            <li id="India/Sub Item 2">Sub Item 2</li>
            <li id="India/Sub Item 3">Sub Item 3</li>
            <li class="additem">+</li>
         </ul>
           </div>
         </li>
       <li>id="Austria"</li>
  </ul>
</div>

就像您对这段代码所期望的那样:

$('.subitems ul li').mousedown(function (event) {
    if (event.which == 3) {
        alert('right mouse clicked');
    }
});

就是当India/SubItem1被点击时,India<li>和 theIndia/SubItem1 <li>都被解雇了,而我只想India/SubItem1被解雇。当点击印度时,我只想让印度开火。

我该如何解决这个问题?

4

1 回答 1

1

event.stopPropagation();将防止事件冒泡 DOM:

$('.subitems ul li').mousedown(function (event) {
    event.stopPropagation();
    if (event.which == 3) {
        alert('right mouse clicked');
    }
});
于 2013-08-01T20:30:41.423 回答