admin 管理员组

文章数量: 1184232

I've got several list items, when I click on the item I want the browser to redirect to ".title > a" link (href). But I don't want any event on the "notThis" selector.

see the example /

<div class="item">
<div class="title">
    <a href="www.jsfiddle">jsfiddle</a>     
</div>
<div> djføljdsaføljdsf a</div>
<div> djføljdsaføljdsf a</div>
<div> djføljdsaføljdsf a</div>
<div class="notThis">
    <a href="/test.html">link1 </a>
    <a href="/test2.html">link2</a>                
</div>

script

​$(document).on('click', '.item', function(event) {
    window.location.href = $(event.currentTarget).find('.title > a').attr('href');        
});​

I've tried :not('.notThis') without any luck.

Changes Thanks for all the answers, but I found another problem. If I have a event handler on the whole item , I can't manage to click on the link in "notThis" selector, because it returns only "false". Isn't there a way to use .not / :not combined with $(document).on('click', -------)

I've got several list items, when I click on the item I want the browser to redirect to ".title > a" link (href). But I don't want any event on the "notThis" selector.

see the example http://jsfiddle.net/VTGwV/29/

<div class="item">
<div class="title">
    <a href="www.jsfiddle.net">jsfiddle.net</a>     
</div>
<div> djføljdsaføljdsf a</div>
<div> djføljdsaføljdsf a</div>
<div> djføljdsaføljdsf a</div>
<div class="notThis">
    <a href="/test.html">link1 </a>
    <a href="/test2.html">link2</a>                
</div>

script

​$(document).on('click', '.item', function(event) {
    window.location.href = $(event.currentTarget).find('.title > a').attr('href');        
});​

I've tried :not('.notThis') without any luck.

Changes Thanks for all the answers, but I found another problem. If I have a event handler on the whole item , I can't manage to click on the link in "notThis" selector, because it returns only "false". Isn't there a way to use .not / :not combined with $(document).on('click', -------)

Share Improve this question edited Jun 15, 2012 at 13:18 Plexus81 asked Jun 15, 2012 at 12:32 Plexus81Plexus81 1,2516 gold badges24 silver badges46 bronze badges 3
  • Are you talking about the return false in my solution? Just use return; then. Of course you can use :not with .on but it will not help you in your situation. – Felix Kling Commented Jun 15, 2012 at 13:23
  • As I said, change return false; to return;: jsfiddle.net/VTGwV/30 – Felix Kling Commented Jun 15, 2012 at 13:32
  • "If I have a event handler on the whole item , I can't manage to click on the link in "notThis" selector" - try my variant, there is no such problem in it. Clicks are captured on wanted elements only. – Artem Koshelev Commented Jun 15, 2012 at 16:53
Add a comment  | 

3 Answers 3

Reset to default 19

You can test whether the click event originated from within the .notThis element (or the element itself):​

$(document).on('click', '.item', function(event) {
    if($(event.target).closest('.notThis').length > 0) {
        return false; // if you want to ignore the click completely
        // return; // else
    }
    window.location.href = $(event.currentTarget).find('.title > a').attr('href');
});​

I also think you can use this instead of event.currentTarget.

Your syntax is wrong, just use the selector like this:

Example

$("div.item > div:not(.notThis)").click(function(){
    window.location.href = $(this).find('.title > a').attr('href');
});

http://jsfiddle.net/Lcg49/4/

var clickable = $('.item').find('div').not('.notThis');

$(clickable).on('click', function(event) {
    alert($(this).parent().find('.title a').attr('href'));
});

本文标签:

Error[2]: Invalid argument supplied for foreach(), File: /www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm, Line: 58
File: /www/wwwroot/roclinux.cn/tmp/route_read.php, Line: 205, include(/www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm)
File: /www/wwwroot/roclinux.cn/tmp/index.inc.php, Line: 129, include(/www/wwwroot/roclinux.cn/tmp/route_read.php)
File: /www/wwwroot/roclinux.cn/index.php, Line: 29, include(/www/wwwroot/roclinux.cn/tmp/index.inc.php)