gpt4 book ai didi

javascript - 为什么 jquery 点击事件在 Firefox 上没有触发?

转载 作者:行者123 更新时间:2023-11-28 07:03:07 24 4
gpt4 key购买 nike

我在 JS 中有这个函数:

    jQuery(function ($) {
$(document).on("click", ".botaoExcluirRecibos", function (e) {
e.preventDefault();
alert("Fired!");
});
});

在我的 asp.net 页面中,我有这个按钮:

<div class="listaExcluir" id="listaExcluir">
<ul id="listaArquivos">
<li>
<div class="voceAnexou"></div>
<div class="divInformacoesAtendimento divInformacoesAtendimentoTabelaRecibo">
<p>Você anexou:<strong> file1.png </strong></p>
<button class="botaoVermelhoPequeno botaoExcluirRecibos" onclick="return false;">Excluir</button>
</div>
</li>
</ul>
</div>

按钮动态添加在li标签上,用户可以添加更多按钮。

它不会在 FIREFOX 上触发点击事件,但在 CHROME 上会触发。

观察:我必须在按钮上添加内联 onclick 事件以防止回发问题。

最佳答案

首先,你可以添加“return false;”到您的事件处理程序,它将避免回发 - 您不需要将其内联。

其次,我推荐一个绑定(bind)函数:

function BindClick(){
$(".botaoExcluirRecibos").unbind("click"); // To prevent double-binding click events!
$(".botaoExcluirRecibos").on("click", function(e){
e.preventDefault();
console.log("Fired!"); //Use the console instead of alerts for debugging purposes!
return false;
});
}

现在,只需调用:

BindClick();

每次向页面添加控件时都需要此事件处理程序。这应该适用于每个浏览器。

关于javascript - 为什么 jquery 点击事件在 Firefox 上没有触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31924770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com