event.target jquery on() mousedown/up 没有给出选择器指定的 dom

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9838137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:22:28  来源:igfitidea点击:

event.target jquery on() mousedown/up is not giving the dom specified by selector

jqueryjquery-eventsevent-bubbling

提问by Nik So

I have this simple html:

我有这个简单的 html:

<div class="wrapper">
    <span class="higher">
        [higher]
        <span class="lower">
            hello
        </span>
    </span>
</div>?

and this js:

和这个js:

$('.wrapper').on('mousedown', '.higher', function(e){
    alert($(e.target).attr('class'))
    })?

Basically, when I click on the "hello" word, I am getting the "lower" instead of "higher". How can I make it so that the event's target is the same as specified in the selector parameter of the on()?

基本上,当我点击“你好”这个词时,我得到的是“较低”而不是“较高”。我怎样才能使事件的目标与 的选择器参数中指定的相同on()

Here's a little fiddle for this http://jsfiddle.net/TUvB7/2/

这是这个http://jsfiddle.net/TUvB7/2/的一个小小提琴

回答by Simon Edstr?m

Use thisinsteed, eis the element that started the bubbling

使用thisinsteed,e是开始冒泡的元素

$('.wrapper').on('mousedown', '.higher', function(e){
    alert($(this).attr('class')) // Changed line
})?

http://jsfiddle.net/TUvB7/7/

http://jsfiddle.net/TUvB7/7/

回答by Joseph

why need e.target?

为什么需要e.target

$('.wrapper').on('mousedown', '.higher', function(e){
    alert($(this).attr('class'))
})?