javascript - 在文档上使用 jquery 添加点击事件跟踪器时,链接上的点击事件不起作用

标签 javascript jquery html

我有一个文件列表,并且可以选择删除该文件。因此,单击删除后,它会要求确认。单击确认后,文件将被删除。 请检查这个 jsfiddle

https://jsfiddle.net/d6ds7qL2/1/

当我添加 html 点击处理程序以便将确认文本更改为原始删除文本时,

$('html').click(function(event){
     if($('.confirmDelete').is(':visible')){
         $('.confirmDelete').hide();
         $('.deleteLink').show();
         return false;
     }
});
$('.confirmDelete').hide();
$('.deleteLink').click(function (event) {
    $(this).hide();
    $('.confirmDelete').filter(':visible').each(function () {
        $(this).hide();
        $(this).prev('.deleteLink').show();
    });
    $(this).next('.confirmDelete').show();
    event.stopPropagation();
    return false;
});

https://jsfiddle.net/d6ds7qL2/2/

但现在的问题是单击“确认”文本时,它不会跳转到 href,而是隐藏确认文本并显示删除文本。

最佳答案

尝试用 document 代替 html

$(document).not(".confirmDelete").click(function(event){
    if($('.confirmDelete').is(':visible')){
        $('.confirmDelete').hide();
        $('.deleteLink').show();
        return false;
    }
});
$('.confirmDelete').hide();
$('.deleteLink').click(function (event) {
    $(this).hide();
    $('.confirmDelete').filter(':visible').each(function () {
        $(this).hide();
        $(this).prev('.deleteLink').show();
    });
    $(this).next('.confirmDelete').show();
    event.stopPropagation();
    return false;
});

fiddle :https://jsfiddle.net/945j4g0x/

关于javascript - 在文档上使用 jquery 添加点击事件跟踪器时,链接上的点击事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32693169/

相关文章:

JavaScript 查找内联元素的 X,Y 以在其旁边设置弹出框

javascript - 如何在 ReactJs 中追加到表中

javascript - 使用javascript在CSS中获取@font_face标签的url

php - 对 S3 图像文件的发布请求

javascript - 来自 jsfiddle 的代码不起作用

html - 反转侧边栏的 Bootstrap 行行为

javascript - <div> 内容未在 &lt;iframe&gt; 中加载

javascript - Node js 上的 Cheerio 出错

css - 带媒体查询的全高

html - 添加显示 : block 后表变小