选择DIV,但不选择其中的链接,jQuery问题 [英] select the DIV but not the link inside it, jQuery question

查看:62
本文介绍了选择DIV,但不选择其中的链接,jQuery问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下div:

<div class='amazingmenu' id='menupull'>
    <img src='images/down.png' width=20 height=20 align='right'/>
    <div id='menupullhideme'>click the arrow to read the rest!<br /></div>
    more jokes on <a href='http://www.amazingjokes.com'>amazingjokes.com</a>
</div>

单击DIV或图像应在jQuery中实现某些功能:

clicking the DIV or the image should execture some function in jQuery:

$('#menupull').click( function() {
          alert( 'pullmenu clicked' );
});

我要这样做,以便仅在单击DIV或图像时才警告该消息,而在单击链接时不发出警告.试过这个:

I want to make it so that it only alerts the message when I click the DIV or the image, but not the link. Tried this:

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

但是没有用.有什么想法吗?

but that didn't work. Any ideas?

推荐答案

问题可能是事件冒泡.您可以做的是在"click"事件内部检查nodeName/tagName是否为A或如果不是这样,则中止其余功能,让锚直接完成任务.

The problem is probably event bubbling.. what you could do, is inside your "click" event check if the nodeName/tagName is A or not, if it is, abort the rest of your function and let the anchor just do it's thing.

我的jQuery有点生锈,因为我自己用的不多,但我认为是这样.

My jQuery is a little rusty as I don't use it much myself, but I think something like this..

$('#menupull').click( function(e) {
    var target  = $(e.target);
    if( target.is('a') ) {
        return true; // True, because we don't want to cancel the 'a' click.
    }
});

这篇关于选择DIV,但不选择其中的链接,jQuery问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆