开发者

Is this possible to force execution of .click() for all elements, even those that are underneath?

开发者 https://www.devze.com 2023-01-16 13:51 出处:网络
Consider this code: HTML: <div class=\'a\'>a</div> <div class=\'b\'>b</div> <div id=\'log\'></div&g开发者_开发问答t;

Consider this code:

HTML:

<div class='a'>a</div>
<div class='b'>b</div>
<div id='log'></div&g开发者_开发问答t;

CSS:

.a, .b {
    width: 100px;
    height: 100px;
    border: 1px solid black;
    position: absolute;
    text-align: right;
}
.a {
    left: 100px;
    top: 100px;
}
.b {
    left: 150px;
    top: 150px;
}

JS:

$('*').click(function(e) {
    log(this.nodeName + ' ' + this.className);
});

function log(s) {
    $('#log').append(s + '<br />');
}

If the intersection is clicked, .click() for .a is not called.

Is there any built-in method to force the execution of click() for all elements, and not only the top one and its parents, or I must implement this myself ?


I think that the behavior you are observing is correct. When clicking the topmost element the one beneath won't get a click event despite it looks it is in the "clicked" area. As a workaround you can manually trigger it's click event: $('.a').click().

Calling click without arguments is equivalent to trigger('click') which raises the specified event. You can find more info in the trigger help topic.

0

精彩评论

暂无评论...
验证码 换一张
取 消