gpt4 book ai didi

javascript - Jquery只执行一次函数

转载 作者:行者123 更新时间:2023-12-03 02:08:41 25 4
gpt4 key购买 nike

我有代码:

$(Page).on('click', '*', function(e)
{
myFunction();
}

function myFunction(){//do something...}

我希望函数中的代码只执行一次,现在当我单击时,例如,5 次,函数 myFunction() 中的代码将被执行 5 次。我不想更改 on('click', '*', ...

最佳答案

使用 jQuery 的 one 方法。

Description: Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

只需将 .on 更改为 .one

$(Page).one('click', '*', function(e) {
myFunction();
});

function myFunction() {
// do something
}

Here's the documentation 一个

关于javascript - Jquery只执行一次函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49677596/

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