解决方法
$(document).ready(my_function);
要么
$(document).ready(function () { // Function code here. });
或者更短但更不易读的变体:
$(my_function);
所有这些将导致在DOM加载后调用my_function。
有关更多详细信息,请参阅ready event文档。
Binds a function to be executed
whenever the DOM is ready to be
traversed and manipulated.
编辑:
要模拟点击,请使用无参数的click()方法:
$('#button').click();
从docs:
Triggers the click event of each
matched element. Causes all of the
functions that have been bound to that
click event to be executed.
为了将它们放在一起,以下代码模拟文档完成加载时的点击:
$(function () { $('#button').click(); });