gpt4 book ai didi

javascript - 哪个是一个接一个地调用两个点击事件的正确方法?

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

我想在点击按钮时触发点击事件。我在下面编写了代码,两者都对我有用,但我想知道哪个更好。

第一种方法:

 jQuery( "#responav li" ).click(function() {
jQuery( "#close-icon" ).click();
});

第二种方法:

jQuery( "#responav li" ).click(function() { 
jQuery( "#close-icon" ).trigger("click");
});

最佳答案

$.trigger('click') 性能更高一些,因为 $.click() 只运行 $.trigger('click')

来自 https://api.jquery.com/click/

This method is a shortcut for [...] .trigger( "click" )

来自http://api.jquery.com/trigger/

Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user.

关于javascript - 哪个是一个接一个地调用两个点击事件的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42543019/

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