[英]Adding JS function to onClick event
I am currently trying to make three buttons that will call the same function, but will result with three different outcomes.我目前正在尝试制作三个按钮,它们将调用相同的 function,但会产生三种不同的结果。 This is what I have so far:
这是我到目前为止所拥有的:
html html
<div class="button-wrapper">
<button class="btn btn-success" onClick="progressStatus(load)">Loading</button>
<button class="btn btn-warning" onClick="progressStatus(warning)">Slow Connection</button>
<button class="btn btn-danger" onClick="progressStatus(error)">Error Message</button>
</div>
<div class="progress-margin">
<h2 id="progress-h2">Your Progress</h2>
<div id="progress-bar">
<div id="myBar"></div>
</div>
</div>
JS JS
function progressStatus(status) {
let elem = document.getElementById("myBar")
let width = '';
let bgColor = '';
let innerText = '';
// if error
if (status==error) {
width = 100;
bgColor = red;
innerText = "Something is wrong! Error";
}
// if slow
if (status==warning) {
width = 100;
bgColor = yellow;
innerText = "Slow Connection";
}
// if load
if (status==load) {
function frame() {
}
elem.style.width = width;
elem.style.backgroundcolor = bgColor;
elem.innerHtml = innerText;
console.log() = status;
}
}
Now, before I even continue working with the code, I wanted to see if I could get the click event on the error button to just register a console.log().现在,在我什至继续使用代码之前,我想看看我是否可以在错误按钮上获得单击事件以仅注册一个 console.log()。 But, unfortunately, I'm getting the following error:
但是,不幸的是,我收到以下错误:
ReferenceError: progressStatus is not defined
at HTMLButtonElement.onclick (/:14:76)
I know that I'm missing something.我知道我错过了什么。
A seperate issue I see is that the JavaScript thinks that you are talking about variables when you type load
, warning
and error
.我看到的另一个问题是 JavaScript 认为您在键入
load
、 warning
和error
时正在谈论变量。 Rather use double quotes around each one to define the text as a string:而是在每个文本周围使用双引号将文本定义为字符串:
if (status=="load") {
function frame() {
}
In the mean time, check that your script file is correctly linked.同时,检查您的脚本文件是否正确链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.