【发布时间】:2016-01-27 17:58:46
【问题描述】:
我有一组看起来像这样的 div
<div class="videoThumbnail" data-videoid="4">
<div class="video"><img src="some/Url" alt="video thumbnail preview"/></div>
<div class="title">I'm a video</div>
<div class="download"><span class="ui-icon ui-icon-arrowthickstop-1-s"></span></div>
</div>
然后我将一个事件绑定到videoThumbnail 类。该功能运行良好,但我现在需要为单击download 绑定另一个事件。我的两个绑定事件如下所示。
//Bind selecting a thumbnail to change the video
$( '.videoThumbnail' ).not( '.download' ).on( 'click', function ( t ) {
alert('you are doing thumbnail stuff');
} );
//Bind download on the litle download icon
$( '.download' ).on( 'click', function ( t ) {
alert('you want to download this');
} );
我希望videoThumbnail 上的第一个绑定事件仅在未选择其中的download 时发生。不幸的是,每当我按下download 图标时,这两个功能都会执行。如何将第一个事件绑定到 videoThumbnail 而不将其绑定到其中的 download?
【问题讨论】:
-
停止.download上点击事件的传播
-
由于第一次点击功能只适用于两个元素你也可以直接绑定它们
$( '.videoThumbnail .video, .videoThumbnail .title' ).on( 'click', function ( t ) { alert('you are doing thumbnail stuff'); } ); -
@miguelmpn 虽然这可以解决这个问题,但我也将它们放在 for 循环中,并且页面上有 2 组它们,但我认为这些信息与问题无关,所以我将其省略了。
-
你可以有不同的孩子吗?但至少
.download始终存在? -
你是对的,它总是相同的类,所以这也是一个可行的答案
标签: javascript jquery html