简体   繁体   English

我不知道如何使我的javascript onclick事件正常工作

[英]I can't figure out how to get my javascript onclick event to work

When I click on the button nothing happens unless I call the function in the html. 当我单击按钮时,除非我在html中调用该函数,否则什么也不会发生。 I am trying to remove all inline javascript. 我正在尝试删除所有内联JavaScript。 I have included the commented-out section of html that works. 我已经包含了html的注释掉部分。 Thanks in advance for the help! 先谢谢您的帮助!

JavaScript: JavaScript:

var welcomeString;
const TEST = 1;

function setWelcomeString() {
    "use strict";
    welcomeString = prompt("Please enter your name: ", "nobody");
}

function writeOutput() {
    "use strict";
    document.getElementById("demo").innerHTML = "Hello, " + welcomeString;
}

function main() {
    "use strict";
    setWelcomeString();
    document.getElementById("sayHi").onclick = writeOutput();
}

main();

HTML: HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>**SET TITLE**</title>
        <link href="css/style.css" rel="stylesheet">
        <script src="firstScript.js"></script>
    </head>

    <body>
        <h1 id="wiggles">TEMPLATE</h1>
        <!--<button id="sayHi" onclick="writeOutput()">Say Hi</button>-->
        <button id="sayHi">Say Hi</button>
        <p id="demo"></p>
    </body>
</html>

When you assign a function as a handler for an event, you need to assign the function itself, not execute the function and assign it's return value as you are doing. 当您将函数分配为事件的处理程序时,您需要分配函数本身,而不是执行函数,并在执行操作时分配其返回值。 Change this: 更改此:

document.getElementById("sayHi").onclick = writeOutput(); // parens

To this: 对此:

document.getElementById("sayHi").onclick = writeOutput; // no parens

Here is a jsfiddle link with a working example. 这是带有工作示例的jsfiddle链接

You have to add your whole code inside load , like this 您必须像这样将整个代码添加到load

window.load = function(){
    // your javascript here
};

Also, as jbabey mentioned, use either 另外,如jbabey所述,请使用

document.getElementById("sayHi").onclick = function(){ writeOutput();};

Or 要么

document.getElementById("sayHi").onclick = writeOutput;

You have two issues. 你有两个问题。

The first being covered by jbabey. 第一个被jbabey覆盖。

The second is that firstScript.js appears before your button. 第二个是firstScript.js出现在您的按钮之前。 This is problematic as when you are assigning the onClick handler to it. 这是有问题的,因为当您为其分配onClick处理程序时。 It doesn't exist in the dom. 它在dom中不存在。

Try putting the entire main script inside window.onload = function () { ... } or moving it to the bottom of the markup. 尝试将整个主脚本放入window.onload = function () { ... }或将其移至标记的底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法弄清楚如何将我的 JavaScript 链接到我的 html 中 - Can't figure out how to link my JavaScript into my html 我不知道我的 JavaScript 函数不起作用 - I can't figure out my JavaScript function is not working 我不知道如何使用 Firebase 返回的 object 获取查询 - I can't figure out how to work with the object returned by Firebase get query 我不知道如何让这些颜色变量在绘图 function 中工作 - I can't figure out how to get these color variables to work in the draw function 我如何弄清楚Javascript在改变我的重点 - How can I figure out where Javascript is changing my focus 我不知道的JavaScript难题 - JavaScript Puzzle I can't figure out 如何让 onclick 事件用于更改 CSS 属性? - How can I get onclick event to work for changing CSS attributes? 我无法弄清楚为什么我添加事件处理程序的简单尝试无效 - I can't figure out why my simple attempt at adding an event handler isn't working 我的 javascript 代码不会将元素推送到我的数组,我不知道为什么? - My javascript code won't push elements to my array and I can't figure out why? 我似乎无法弄清楚如何修复JavaScript中的两个棉绒。 有人可以帮我理解吗? - I can't seem to figure out how to fix two lints in my javascript. Can someone help me understand?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM