gpt4 book ai didi

javascript - jquery 函数适用于 live,不适用于 on

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:51:22 26 4
gpt4 key购买 nike

尝试使用首选 on 方法调用,但代码不适用于 on 但适用于 live 方法。

好的,我有一个简单的按钮元素。

<button id="EditVal" name="EditVal" style="btn" value="Edit Debt">Edit Debt </button>

如果我使用 live 方法,此代码有效:

$("#EditVal").live("click", function(e){
alert('Edit this Val');
})

但这不是

$("#EditVal").on("click", function(e){
alert('Edit this Val');
})

我做错了什么?

最佳答案

您正在使用 on()喜欢bind() ,不像live() .要像 live() 一样使用它,您应该这样写:

$(document).on("click", "#EditVal", function(e) {
alert('Edit this Val');
});

请注意,出于性能原因,最好在非动态祖先元素上调用 on() 而不是 document,即像 delegate() 一样使用它而不是 live()

关于javascript - jquery 函数适用于 live,不适用于 on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8927966/

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