HTML正文中的Javascript函数,将无法正常工作 [英] Javascript function in html body, wont work

查看:103
本文介绍了HTML正文中的Javascript函数,将无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < body onLoad =display(());}在我的文档中,我有一个onLoad函数, )> 

另外,我在文档的末尾添加了一个函数,它改变了一些CSS属性:

 < script> 
window.onload = foo(),bar();
< / script>

不知怎的,整件事情都行不通!我试图在文档的末尾添加所有函数,但我没有得到它,但它们不会触发!

p>它会调用这些函数,但它不会为<$​​ c $ c> window.onload 分配任何有用的内容,除非您的上一个函数恰好返回一个函数。



您需要为 window.onload 分配一个函数,该函数将在窗口已准备就绪。

  window.onload = function(){
foo();
bar();
};

此外,由于您将脚本放在底部,因此您可能不需要 window.onload

 < script> 
foo();酒吧();
< / script>
< / body>






您还应该知道直接分配给 window.onload 会覆盖在< body onLoad = ...> 中分配的脚本,所以你不应该'两个都做。目前, bar(); 的返回值正在清除调用 display(); 的函数。



摆脱 window.onload 赋值可以解决这个问题。


In my document I have an onLoad function given like here in this example:

<body onLoad="display()">

In addition I added a function at the end of the document which changes some CSS properties:

<script>
window.onload = foo(), bar();
</script> 

Somehow the whole thing doesn't work! I tried to add all functions at the end of the document but I don't get it, somehow they don't trigger!

解决方案

That will invoke the functions, but it doesn't assign anything useful to window.onload unless your last function happens to return a function.

You need to assign a function to window.onload that will invoke your functions when the window is ready.

window.onload = function() {
    foo();
    bar();
};

Plus, since you're putting the script at the bottom, you probably don't need the window.onload.

    <script>
        foo(); bar();
    </script>
</body>


You should also be aware that assigning directly to window.onload will overwrite the script assigned in the <body onLoad=...>, so you shouldn't do both. Currently, the return value of bar(); is wiping out the function that invokes display();.

Getting rid of the window.onload assignment will fix that.

这篇关于HTML正文中的Javascript函数,将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆