jQuery 如何将此 onclick() 函数转换为 onload()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14453145/
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
How to convert this onclick() function to onload()
提问by Aviral
Possible Duplicate:
How to make onclick automatically through onload function
So I have the following function which is being called when I click a button. Here is the code:
所以我有以下功能,当我点击一个按钮时会被调用。这是代码:
<a href="#" role="button" onClick="myFunction('parameter')">
How can I call this function as soon as page is loaded? I tried onLoad function but doesn't seem to work.
如何在页面加载后立即调用此函数?我试过 onLoad 函数,但似乎不起作用。
采纳答案by viki
There are multiple options for onload event.
onload 事件有多个选项。
In HTML
在 HTML 中
<body onload="myFunction('parameter')">
In Javascript
在 JavaScript 中
window.onload=function(){
myFunction('parameter');
};
In JQuery
在 JQuery 中
$(document).ready(function(){
myFunction('parameter');
});
回答by Samuel Liew
Put this anywhere on your page, preferably in the <head>
把它放在你页面上的任何地方,最好放在 <head>
<script type="text/javascript">
$(function() {
myFunction('parameter');
});
</script>
See https://stackoverflow.com/a/7911809/584192for more ways of doing it via jQuery.
有关通过 jQuery 执行此操作的更多方法,请参阅https://stackoverflow.com/a/7911809/584192。
回答by phnkha
Have you tried this:
你有没有试过这个:
window.onload = function(){
myFunction('parameter');
}
See more at: https://developer.mozilla.org/en-US/docs/DOM/window.onload
查看更多信息:https: //developer.mozilla.org/en-US/docs/DOM/window.onload
回答by Bowen Li
You can use the following code.
您可以使用以下代码。
<script>
onload = function(){
myFunction('parameter');
}
</script>
回答by Adil
You call in myFunction document.readyor just before closing of body tag.
您调用 myFunction document.ready或在 body 标记关闭之前调用。
Within document.ready
在 document.ready 中
<script type="text/javascript">
$(document).ready(function(){
myFunction('parameter');
});
</script>
Before closing body tag
在关闭 body 标签之前
<script type="text/javascript">
myFunction('parameter');
</script>