简体   繁体   English

jQuery按钮单击事件被另一个单击事件覆盖

[英]Jquery button click event is overwritten by another click event

I'm building mobile theme using jquery-mobile. 我正在使用jquery-mobile构建移动主题。

//Swipe to reveal delete icon
$(".swipe-delete li").on("swipe", function(e) {
    $(".swipe-delete .delete").remove();
    $(this).find(".ui-btn-inner").append('<div class="delete"><a href="#">Delete</a></div>');
});

//Click to delete fav from list
$(document).on("click", ".swipe-delete .delete a", function() {
    $(this).closest("li").remove();
});

//Currently playing
$(".ui-listview li").on("click", function() {
    $(".ui-listview .playing").remove();
    $(".swipe-delete .delete").remove();
    $(this).find(".ui-btn-inner").append('<div class="playing"><a href="#">Playing</a></div>');
});

So, I'm implementing swipe to delete a button. 因此,我正在实施滑动以删除按钮。 It works correctly until I enable currently playing section. 在我启用当前正在播放的部分之前,它可以正常工作。 When it is enabled, whenever I try to click the delete button the event is intercepted by currently the playing function and method remove() doesn't get invoked. 启用该功能后,每当我尝试单击Delete按钮时,当前的播放功能都会拦截该事件,并且不会调用remove()方法。

To see it in action please visit this site . 要查看它的实际效果,请访问此网站

And click "favorites" in the footer. 然后单击页脚中的“收藏夹”。 Swipe on the listview to show delete button. 在列表视图上滑动以显示删除按钮。 Try to click it and you will see that play button appears instead of removing it. 尝试单击它,您将看到出现播放按钮,而不是将其删除。

Since the delete handler is bound to the document, the event doesn't reach there until after the click handler for the "currently playing" section has run, removing the element that would cause the event. 由于删除处理程序已绑定到文档,因此直到“当前正在播放”部分的单击处理程序运行后,事件才会到达该位置,并删除将导致事件的元素。 There are a few ways to go about fixing this, but this is probably the simplest: 有几种解决方法,但这可能是最简单的:

//Currently playing
$(".ui-listview li").on("click", function(e) {

    // if the target of the event was the delete button, don't to anything
    if ($(e.target).is('.swipe-delete .delete a')) { return; }

    $(".ui-listview .playing").remove();
    $(".swipe-delete .delete").remove();
    $(this).find(".ui-btn-inner").append('<div class="playing"><a href="#">Playing</a></div>');
});

You can use the stopPropagation() method. 您可以使用stopPropagation()方法。

So change your remove function to this: 因此,将您的删除功能更改为:

$(document).on("click", ".swipe-delete .delete a", function(e) {
    e.stopPropagation();
    $(this).closest("li").remove();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM