gpt4 book ai didi

jquery - 在jquery中编写onclick事件的适当位置

转载 作者:行者123 更新时间:2023-12-01 03:43:31 26 4
gpt4 key购买 nike

我正在 jquery onready 函数之外编写 jquery onclick 事件。如果我在 jquery onready 函数中编写 onclick 函数,它的执行流程有何不同?

例如:

  1. jQuery(document).ready(function(){

});
jQuery("#elementid").click(function(){
});

2. jQuery(document).ready(function(){
jQuery("#elementid").click(function(){
});

});

1和2的执行流程有何不同。哪一种是使用实时的最佳方式?
提前致谢。

最佳答案

第二个更好。第一个有时可能不起作用,因为您可能选择了尚未加载的元素。通过包含在文档就绪处理程序中,您可以确保所有页面元素在引用它们之前都已加载。

更一般地说...

// code that does not interact with DOM can go here, and will be executed
// before the has DOM finished loading.
jQuery(document).ready(function(){
// code that needs to access the page elements/DOM goes here, and will execute
// after the DOM has finished loading
})

关于jquery - 在jquery中编写onclick事件的适当位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16143689/

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