gpt4 book ai didi

javascript - 如何在添加新的 onClick 事件之前清除 onClick?

转载 作者:行者123 更新时间:2023-12-01 03:48:40 27 4
gpt4 key购买 nike

我尝试使用 JavaScript 将 onclick 事件替换为其他 onclick 事件:

<button id='myButton' onClick=""/>

旧 onClick 事件:

$('#myButton').click(function(){
alert('1');
});

然后我做同样的事情并更改警报的值,我确实喜欢这样:

$('#myButton').click(function(){
alert('2');
});

The result of method above is alert show twice for 1 and 2. What i want is only 2 that must show (i replace alert('1') with alert('2'); not add another alert. How to fix my code?

最佳答案

试试这个:

$(document).off('click', '#myButton').on('click', '#myButton', function(){
alert('2');
});

它将取消绑定(bind)先前的事件监听器并添加新的事件监听器。

关于javascript - 如何在添加新的 onClick 事件之前清除 onClick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43382118/

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