gpt4 book ai didi

javascript - jquery 中的 Click 事件是永久的吗

转载 作者:行者123 更新时间:2023-11-28 19:49:59 25 4
gpt4 key购买 nike

你问永久是什么意思,我也不知道,这只是描述我的情况最准确的。
我有这个 html 按钮:

<button type="button" 
class="btn btn-primary btn-lg" data-follow="follow"
data-target="52cd6196211b6fc26a8b456c">

<span class="glyphicon glyphicon-plus"></span>
Follow
</button>

我添加了一个点击事件,如下:

 $("button[data-follow=follow]").click(function(e) {//scenario1});

在给定情况下,执行此代码:

 $("button[data-follow=follow]").attr("data-follow","unfollow");

我有这个监听器:

$("button[data-follow=unfollow]").click(function(e) {//scenario2});

当我再次单击按钮时,它会触发场景 1,什么时候应该触发场景 2?

有人知道发生了什么事吗?

最佳答案

然后我们必须使用event-delegation在此背景下,

$(document).on('click',"button[data-follow=follow]",function(e) {//scenario1});

$(document).on('click',"button[data-follow=unfollow]",function(e) {//scenario2});

请参阅我建议的document来实现事件委托(delegate),但您应该使用相对于提供的选择器而言最接近的静态父级。

关于javascript - jquery 中的 Click 事件是永久的吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23649946/

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