我在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/

10-12 03:07
查看更多