English 中文(简体)
jQuery,使用正确的选择器
原标题:jQuery, use the right selector

我有一个div,里面有一些文本和一些单词,它们被某些span标记包围。我想点击那些“跨区”单词,让div向下滑动,显示一个“底层”div,其中包含与某个跨区单词相关的文本。当我点击div而不是跨区单词时,我希望div向后滑动。因此,我将一些操作绑定到div和这些跨度的类。但是,当我点击一个span时,div也会得到点击操作。当我点击span时,我应该如何更改选择器以不接收div上的点击操作?

这是代码:

<div class= mainpage >
    <p class= common >bla-bla-bla <span class= footnoted  footnote= f1 >foo bar</span></p>
</div>

jQuery代码:

$(document).ready(function(){
    $(".mainpage>.common>.footnoted").click(function(){
        alert( a );
    });

    $(".mainpage").not($(".mainpage>.common>.footnoted")).click(function(){
        alert( b );
    });
});

换句话说,当我点击“foo bar”时,我会得到“a”和“b”警报,而我只想得到“a”。我该怎么做?

Thank you in advance. Timofey.

最佳回答

你需要使用

event.stopPropagation();

在这样的跨度

$( div ).click(function(){alert( div clicked );});

$( div span ).click(function(event){ 
    alert( span clicked )
    event.stopPropagation();
});

现场演示

引用

问题回答

根据最先触发的事件,您可以尝试添加<code>return false添加到单击事件函数的末尾。这应该会阻止第二次点击事件的启动并解决您的问题。





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签