gpt4 book ai didi

javascript - 从同一个点击事件中停止两个或多个点击事件

转载 作者:行者123 更新时间:2023-11-29 18:13:48 26 4
gpt4 key购买 nike

我在 jsp 页面中创建了一个按钮,我为同一个按钮编写了两个单击函数,一个带有标签,另一个带有按钮的类/id。现在,当我点击按钮时,两个点击功能都被一个接一个地调用。我的要求是在第一次点击功能中,我想停止第二次点击功能。我不想删除删除第二次点击功能,因为在某些情况下我需要它来执行。

下面是我正在尝试的代码。

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){

$("input[type=button]").click(function(e){

if(true) {
alert("The paragraph was clicked.");
}
else {
// I want to remove the second click function here
}
});

$("#test").click(function(){
alert("Test paragraph was clicked.");

});

});
</script>
</head>
<body>
<input type = 'button' value='test' id='test'/>
</body>
</html>

最佳答案

你需要使用e.stopImmediatePropagation():

$("input[type=button]").click(function(evt){
evt.stopImmediatePropagation();
....
});

这可以防止“横向冒泡”,因为需要一个更好的短语,即绑定(bind)到同一元素的更多事件回调不会触发。

关于javascript - 从同一个点击事件中停止两个或多个点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25061641/

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