使用 body onload 的替代方法
Alternative for using body onload
我是新手,我正在尝试使用 javascript 来实现某些目标。
我有一个功能,我需要在我的页面加载后生效,但我不想使用 <body onload="init()"
还有其他选择吗?
<script type="text/javascript">
function init() {
container = document.createElement('div');
<script>
<body onload="init()">
</body>
您还可以这样做:
window.onload = function() {
init();
}
从 load
处理程序调用它:
function init() {
container = document.createElement('div');
// ...
}
window.addEventListener("load", init);
我是编程新手,但我认为你可以这样做:
随着 Jquery
$(document).ready(function(){
// code loaded..
});
没有Jquery
// this code is bad to practise
<script type="text/javascript">
function init() {
// Put your code here
}
window.onload = init;
</script>
好吧,你可以使用 jquery,但你必须手动或使用
将其包含在你的头脑中
<!-- Google CDN -->
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
如果您想使用 Google CDN,或
<!-- Microsoft CDN -->
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
</head>
用于 Microsoft CDN。
现在,您可以在网页中使用 jquery。所以写在你的脑海里
<script type="text/javascript">
$(window).on('load",function(){
// your code
});
</script>
当然,您需要知道如何使用jquery才能使用此解决方案。但是,你也可以使用javasript,但我建议你开始学习jquery:非常有用。
<script>
window.onload = function() {
//your code
}
</script>
这应该是正确的方法 ;)
非常重要:当您的文档开始加载时将调用此函数!如果你想在页面加载结束时应用一些东西(提高加载速度的更好解决方案)使用
$(document).ready(function(){
//your code
});
在jquery,或
document.addEventListener("DOMContentLoaded", function(event) {
//your code
});
在javascript
我是新手,我正在尝试使用 javascript 来实现某些目标。
我有一个功能,我需要在我的页面加载后生效,但我不想使用 <body onload="init()"
还有其他选择吗?
<script type="text/javascript">
function init() {
container = document.createElement('div');
<script>
<body onload="init()">
</body>
您还可以这样做:
window.onload = function() {
init();
}
从 load
处理程序调用它:
function init() {
container = document.createElement('div');
// ...
}
window.addEventListener("load", init);
我是编程新手,但我认为你可以这样做: 随着 Jquery
$(document).ready(function(){
// code loaded..
});
没有Jquery
// this code is bad to practise
<script type="text/javascript">
function init() {
// Put your code here
}
window.onload = init;
</script>
好吧,你可以使用 jquery,但你必须手动或使用
将其包含在你的头脑中<!-- Google CDN -->
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
如果您想使用 Google CDN,或
<!-- Microsoft CDN -->
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
</head>
用于 Microsoft CDN。 现在,您可以在网页中使用 jquery。所以写在你的脑海里
<script type="text/javascript">
$(window).on('load",function(){
// your code
});
</script>
当然,您需要知道如何使用jquery才能使用此解决方案。但是,你也可以使用javasript,但我建议你开始学习jquery:非常有用。
<script>
window.onload = function() {
//your code
}
</script>
这应该是正确的方法 ;)
非常重要:当您的文档开始加载时将调用此函数!如果你想在页面加载结束时应用一些东西(提高加载速度的更好解决方案)使用
$(document).ready(function(){
//your code
});
在jquery,或
document.addEventListener("DOMContentLoaded", function(event) {
//your code
});
在javascript