0

I`m new to jQuery and would like to know how I can edit a click function.

Here is what the HTML looks like:

<ul class="result">
   <li id="a"></li>
   <li></li> //will be added through a loop depending on input
</ul>

So my problem is that when I will click at the li object it will do something. Now I would like to exclude li id="a" from that event. I thought return false; would handle this but it does not.

Here is what the jQuery function looks like:

$('.result li').click(function() { 
    $('.result li a').click(function (event) {
        event.preventDefault();
        event.stopPropagation();
    });
        some further code...
});

I also tried:

$('.result li').click(function() { 
    $('.result li a').click(function () {
        return false;
    });
        some further code...
});

Thanks alot.

4

5 回答 5

6
$('.result li:not(#a)').click(function() { 
于 2013-04-17T11:45:55.920 回答
2

你可以这样做

$('.result li').click(function() {     
       if($(this).attr('id')!="your id")
        some further code...
    });
于 2013-04-17T11:45:07.660 回答
1

尝试这个:

$('.result li').click(function (e) {
    if (e.target === this) {
        some further code...
    }
});

这里,this表示当前作用域中的元素,这里总是被li点击的。

ande.target表示实际点击的元素,可以是lia

因此,如果实际单击的元素不在li当前范围内,则e.target === this返回 false 并且没有任何反应(没有触发单击事件),反之亦然。

于 2013-04-17T11:53:38.190 回答
0

否定伪类(:not(X))将完成这项工作。您可以在此处对否定伪类有更好的了解

现在请试试这个

$('.result li:not(#a)').click(function() {

    // Your code here...

}
于 2013-04-17T12:33:16.327 回答
0

一个简单的 if 应该可以工作:

$('.result li').click(function(ev) { 
  if (this.id == "a") {
    return; 
  }

  // Do your stuff
});
于 2013-04-17T11:47:34.373 回答