[英]how can I convert a button click with an onload event?
How can I convert this script so that it fires onload rather than using a button click? 如何转换此脚本以便它触发onload而不是使用按钮单击? I'm trying to understand this but it is not sinking in. Thanks
我试图理解这一点,但它并没有陷入其中。谢谢
<script type="text/javascript">
jQuery(function($){
// unordered list
$('button.item').click(function(){
$('ul').foo();
});
});
</script>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
<button class="item">foo</button>
Use the load function to have the function executed on the onload event : 使用load函数在onload事件上执行函数:
<script type="text/javascript">
$(window).load(function(){
$('ul').foo();
});
</script>
Note that if you don't need to have everything loaded (eg sub-elements, mainly images), it's generally better to execute your function as soon as the dom is ready, using $(callback) : 请注意,如果您不需要加载所有内容(例如子元素,主要是图像),那么在dom准备好后,通常使用$(回调)执行您的函数会更好:
<script type="text/javascript">
$(function(){
$('ul').foo();
});
</script>
<script type="text/javascript">
function abc() {
alert('ok');
}
window.onload = abc;
</script>
You can use something like this: 你可以使用这样的东西:
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
<script type="text/javascript">
$(document).ready(function() {
$('ul').foo();
});
</body>
Is better if you call it at the end of the file, just befor closing the <body>
tag. 如果你在文件末尾调用它更好,只需关闭
<body>
标记即可。
And the function $(document).ready(function(){...
is the one you use whn you need to do something when the page loads. 函数
$(document).ready(function(){...
是你在页面加载时需要做的事情。
I recommend you to check the jQuery tutorials in w3schools: http://www.w3schools.com/jquery/default.asp 我建议你查看w3schools中的jQuery教程: http : //www.w3schools.com/jquery/default.asp
Document.ready is faster to react than window.onLoad Document.ready比window.onLoad反应更快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.