onload() 函数拒绝运行?
您好,很抱歉用这么简单的问题打扰您,但是这段代码,我确实找不到任何问题,但没有任何警报触发,所以我知道 init 甚至没有被传递给 窗口.onload。这是代码:
window.onload()=init;
alert("The file has been linked to.");
function init(){
alert("Init has been loaded");
var button = document.getElementById("myButton");
button.onclick = handleButtonClick;
}
function handleButtonClick()
{
alert("button has been clicked");
var target = document.getElementById("target");
var image = document.createElement("<img />");
image.innerHTML = "<img src=../Images/banner_test.jpg alt=Banner height=42 width=42 />" ;
target.appendChild(image);
}
JS 位于我链接到的外部 js 文件中:
<script type="text/javascript" src="../JS/Experiment.js" > </script>
我是否拼写错误或忘记了参数,因为正如我所说,没有一个警报会激活,谈论创建新的 并添加它。
HTML 不是问题我尝试过使用内联 的简单
alert()
并且它可以工作,但我需要将其放在外部文件中。
Hello I am sorry to bother you with such a simple question but this bit of code, I literally could not find anything wrong with it, but none of the alerts even trigger so I know that init isn't even been passed to window.onload
. Here is the code:
window.onload()=init;
alert("The file has been linked to.");
function init(){
alert("Init has been loaded");
var button = document.getElementById("myButton");
button.onclick = handleButtonClick;
}
function handleButtonClick()
{
alert("button has been clicked");
var target = document.getElementById("target");
var image = document.createElement("<img />");
image.innerHTML = "<img src=../Images/banner_test.jpg alt=Banner height=42 width=42 />" ;
target.appendChild(image);
}
The JS is in a external js file I've linked to with:
<script type="text/javascript" src="../JS/Experiment.js" > </script>
Have I misspelt something or forgotten a parameter because as I've said none of the alarms will activate, no use talking about creating a new <img />
and adding it.
The HTML isn't a problem I've tried a simple alert()
with inline <script>
and it works but I need this to be in a external file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
window.onload
不是方法,请删除括号()
window.onload
is not a method, drop the parens()
window.onload
不是一个函数,因此第一行将阻止其余部分运行:应该
注意的是,init 函数也仅可用,因为 JavaScript 将非“var”函数提升到范围的顶部,因此当您指向这样的函数时请注意这一点。
window.onload
isn't a function and so the first line will prevent the rest from running:It should be
note that also the init function is only available because JavaScript hoists non "var"ed functions to the top of the scope so watch out for this when you're pointing to functions like this.
正如其他人所说,必须删除括号。不过,在您的代码正常运行之前,您必须修复更多代码。
document.createElement
接受一个纯字符串作为参数,该参数将是标签名称。您当前的代码将在此时引发错误。innerHTML
属性对其没有任何意义。您必须显式附加属性/属性(见下文)。固定代码:
As others have stated, the parens have to be dropped. Before your code runs fine, you have to fix more code, though.
document.createElement
accepts a plain string as a parameter, which will be the tag name. Your current code will throw an error at that point.innerHTML
property makes no sense for it. You have to explicitly attach attributes/properties to it (see below).Fixed code: