onload() 函数拒绝运行?

发布于 2024-12-27 20:26:25 字数 987 浏览 0 评论 0原文

您好,很抱歉用这么简单的问题打扰您,但是这段代码,我确实找不到任何问题,但没有任何警报触发,所以我知道 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 不是问题我尝试过使用内联

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓味的萝莉 2025-01-03 20:26:25

window.onload 不是方法,请删除括号 ()

window.onload is not a method, drop the parens ()

涫野音 2025-01-03 20:26:25

window.onload 不是一个函数,因此第一行将阻止其余部分运行:

window.onload()=init

应该

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);
 }

注意的是,init 函数也仅可用,因为 JavaScript 将非“var”函数提升到范围的顶部,因此当您指向这样的函数时请注意这一点。

window.onload isn't a function and so the first line will prevent the rest from running:

window.onload()=init

It should be

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);
 }

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.

悟红尘 2025-01-03 20:26:25

正如其他人所说,必须删除括号。不过,在您的代码正常运行之前,您必须修复更多代码。

  1. document.createElement 接受一个纯字符串作为参数,该参数将是标签名称。您当前的代码将在此时引发错误。
  2. 创建图像元素后,innerHTML 属性对其没有任何意义。您必须显式附加属性/属性(见下文)。

固定代码:

function handleButtonClick()
{
    alert("button has been clicked");
    var target = document.getElementById("target");
    //var image = document.createElement("<img />"); // INVALID!!
    var image = new Image(); // or: document.createElement("img");
    //image.innerHTML = "<img src=../Images/banner_test.jpg alt=Banner height=42          width=42 />" ;
    image.src = "../Images/banner_test.jpg";
    image.alt = "Banner";
    image.height = 42;
    image.width = 42;
    target.appendChild(image);
 }

As others have stated, the parens have to be dropped. Before your code runs fine, you have to fix more code, though.

  1. 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.
  2. When an image element has been created, the innerHTML property makes no sense for it. You have to explicitly attach attributes/properties to it (see below).

Fixed code:

function handleButtonClick()
{
    alert("button has been clicked");
    var target = document.getElementById("target");
    //var image = document.createElement("<img />"); // INVALID!!
    var image = new Image(); // or: document.createElement("img");
    //image.innerHTML = "<img src=../Images/banner_test.jpg alt=Banner height=42          width=42 />" ;
    image.src = "../Images/banner_test.jpg";
    image.alt = "Banner";
    image.height = 42;
    image.width = 42;
    target.appendChild(image);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文