jQuery 加载后的jQuery调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/890090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery call function after load
提问by Phill Pafford
Need to call a filter function on some options based on a radio selected (Link here), How can I call this after the page is loaded? the radio is set from a DB call and I would like to filter the options
需要根据选择的收音机(链接在这里)对某些选项调用过滤器功能,加载页面后如何调用它?收音机是从数据库调用设置的,我想过滤选项
回答by Ayman Hourieh
$(document).ready(my_function);
Or
或者
$(document).ready(function () {
// Function code here.
});
Or the shorter but less readable variant:
或者更短但可读性较差的变体:
$(my_function);
All of these will cause my_function to be called after the DOM loads.
所有这些都会导致在 DOM 加载后调用 my_function。
See the ready eventdocumentation for more details.
有关更多详细信息,请参阅就绪事件文档。
Binds a function to be executed whenever the DOM is ready to be traversed and manipulated.
绑定要在 DOM 准备好被遍历和操作时执行的函数。
Edit:
编辑:
To simulate a click, use the click() method without arguments:
要模拟点击,请使用不带参数的 click() 方法:
$('#button').click();
From the 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.
触发每个匹配元素的点击事件。导致所有绑定到该点击事件的函数被执行。
To put it all together, the following code simulates a click when the document finishes loading:
总而言之,以下代码模拟了文档加载完成时的点击:
$(function () {
$('#button').click();
});
回答by afsane
$(window).bind("load", function() {
// code here
});
回答by Splitice
Crude, but does what you want, breaks the execution scope:
粗鲁,但做你想做的事,打破了执行范围:
$(function(){
setTimeout(function(){
//Code to call
},1);
});
回答by Ender
In regards to the question in your comment:
关于您评论中的问题:
Assuming that you've previously bound your function to the click event of the radio button, add this to your $(document).ready
function:
假设您之前已将您的函数绑定到单选按钮的单击事件,请将其添加到您的$(document).ready
函数中:
$('#[radioButtonOptionID]').click()
Without a parameter, that simulates the click event.
没有参数,模拟点击事件。