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 |3 Answers
Reset to default 19You 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'));
});
本文标签:
版权声明:本文标题:javascript - $(document).on('click', selector, function() ) combined with :not - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1738490229a1979478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
return falsein my solution? Just usereturn;then. Of course you can use:notwith.onbut it will not help you in your situation. – Felix Kling Commented Jun 15, 2012 at 13:23return false;toreturn;: jsfiddle.net/VTGwV/30 – Felix Kling Commented Jun 15, 2012 at 13:32