如何使用点击事件监听器显示按钮的值

huangapple go评论55阅读模式
英文:

How to display button value with click eventlistner

问题

我正在尝试创建一个简单的计算器项目,目前我发现在警告框中显示按钮值很困难。我认为在JavaScript事件监听器函数中我犯了一个错误,我只是无法弄清楚。

请帮我查看脚本:

const a = document.querySelectorAll(".drum");
var y = a.length;
for (var count = 0; count < y; count++) {
  var z = a[count];
  var x = z.addEventListener("click", Action);
}

function Action() {
  var displayer = z.target.value;
  alert("Button value :" + displayer);
}

如果您有任何其他问题,请随时提问。

英文:

i am trying to create a simple calculator project , currently i'm finding it difficult to display the button value in an alert box . i think im making a mistake in the javascript event listner function i just can not figure it out .

Please help me with the script:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const a = document.querySelectorAll(&quot;.drum&quot;);
var y = a.length;
for (var count = 0; count &lt;= y; count++) {
  var z = a[count];
  var x = z.addEventListener(&quot;click&quot;, Action);
}

function Action() {
  var displayer = z.target.value;
  alert(&quot;Button value :&quot; + displayer);
}

<!-- language: lang-css -->

body {
  text-align: center;
  background-color: #283149;
}

h1 {
  font-size: 5rem;
  color: #DBEDF3;
  font-family: &quot;Arvo&quot;, cursive;
  text-shadow: 3px 0 #DA0463;

}

footer {
  margin-top: 100px;
  color: #DBEDF3;
  font-family: sans-serif;
}

.pressed {
  box-shadow: 0 3px 4px 0 #DBEDF3;
  opacity: 0.5;
}

.inp{
  width: 346px;
  border-radius: 5px;
  height: 40px;
  text-align: right;
}


.red {
  color: red;
}

.drum {
  outline: none;
  border: 5px solid #404B69;
  font-size: 50px;
  font-family: &#39;Arvo&#39;, cursive;
  font-weight: 100;
  color: #DA0463;
  text-shadow: 3px 0 #DBEDF3;
  border-radius: 15px;
  display: inline-block;
  width: 80px;
  height: 80px;
  text-align: center;
  margin: 5px;
  background-color: white;
}

<!-- language: lang-html -->

&lt;h1 id=&quot;title&quot;&gt;My Calculator ✌&#127996;&lt;/h1&gt;
&lt;div &gt;
  &lt;button class=&quot;drum&quot; value=&quot;1&quot;&gt;1&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;2&quot;&gt;2&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;3&quot;&gt;3&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;+&quot;&gt;+&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;set&quot;&gt;
  &lt;button class=&quot;drum&quot; value=&quot;4&quot;&gt;4&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;5&quot;&gt;5&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;6&quot;&gt;6&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;-&quot;&gt;-&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;set&quot;&gt;
  &lt;button class=&quot;drum&quot; value=&quot;7&quot;&gt;7&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;8&quot;&gt;8&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;9&quot;&gt;9&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;*&quot;&gt;x&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;set&quot;&gt;
  &lt;button class=&quot;drum&quot; value=&quot;0&quot;&gt;0&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;/&quot;&gt;➗&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;.&quot;&gt;.&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;=&quot;&gt;=&lt;/button&gt;
&lt;/div&gt;
&lt;footer&gt;
  Made with ❤️ in Nigeria.
&lt;/footer&gt;

<!-- end snippet -->

I have tried reading on eventlistners but have not gotten the problem.

答案1

得分: 1

我立即看到两个问题,您也可以在浏览器的调试工具中观察到这两个问题。 (问题中的堆栈片段使控制台更可见,但您始终可以在您自己的浏览器工具中查看控制台。 这应该始终是一个立即调试的步骤。)

首先,这立即产生一个错误:

for (var count = 0; count &lt;= y; count++) {
  var z = a[count];
  var x = z.addEventListener(&quot;click&quot;, Action);
}

由于循环条件中使用了 &lt;=,循环的最后一次迭代超出了数组的长度。 数组索引从 0 开始,结束于数组长度的前一位。 将比较改为 &lt;

for (var count = 0; count &lt; y; count++) {
  var z = a[count];
  var x = z.addEventListener(&quot;click&quot;, Action);
}

其次,在这里 z 不是您想象的那样:

function Action() {
  var displayer = z.target.value;
  alert(&quot;Button value :&quot; + displayer);
}

此时,z 是上面循环的最后一个值。 它刚好在数组的外面。 因此,每个事件处理程序都试图使用相同的(不存在的)元素。

幸运的是,您不需要上面循环中的 z。 系统将一个事件对象传递给点击处理程序,您可以使用该对象。 例如,我在这里将该对象称为 e

function Action(e) {
  var displayer = e.target.value;
  alert(&quot;Button value :&quot; + displayer);
}

将它们放在一起:

const a = document.querySelectorAll(&quot;.drum&quot;);
var y = a.length;
for (var count = 0; count &lt; y; count++) {
  var z = a[count];
  var x = z.addEventListener(&quot;click&quot;, Action);
}

function Action(e) {
  var displayer = e.target.value;
  alert(&quot;Button value :&quot; + displayer);
}

body {
  text-align: center;
  background-color: #283149;
}

h1 {
  font-size: 5rem;
  color: #DBEDF3;
  font-family: &quot;Arvo&quot;, cursive;
  text-shadow: 3px 0 #DA0463;
}

footer {
  margin-top: 100px;
  color: #DBEDF3;
  font-family: sans-serif;
}

.pressed {
  box-shadow: 0 3px 4px 0 #DBEDF3;
  opacity: 0.5;
}

.inp {
  width: 346px;
  border-radius: 5px;
  height: 40px;
  text-align: right;
}

.red {
  color: red;
}

.drum {
  outline: none;
  border: 5px solid #404B69;
  font-size: 50px;
  font-family: 'Arvo', cursive;
  font-weight: 100;
  color: #DA0463;
  text-shadow: 3px 0 #DBEDF3;
  border-radius: 15px;
  display: inline-block;
  width: 80px;
  height: 80px;
  text-align: center;
  margin: 5px;
  background-color: white;
}

<h1 id="title">My Calculator ✌&#127996;</h1>
<div >
  <button class="drum" value="1">1</button>
  <button class="drum" value="2">2</button>
  <button class="drum" value="3">3</button>
  <button class="drum" value="+">+</button>
</div>
<div class="set">
  <button class="drum" value="4">4</button>
  <button class="drum" value="5">5</button>
  <button class="drum" value="6">6</button>
  <button class="drum" value="-">-</button>
</div>
<div class="set">
  <button class="drum" value="7">7</button>
  <button class="drum" value="8">8</button>
  <button class="drum" value="9">9</button>
  <button class="drum" value="*">x</button>
</div>
<div class="set">
  <button class="drum" value="0">0</button>
  <button class="drum" value="/">➗</button>
  <button class="drum" value=".">.</button>
  <button class="drum" value="=">=</button>
</div>
<footer>
  Made with ❤️ in Nigeria.
</footer>
英文:

I'm seeing two issues right away, which you can also observe in your browser's debugging tools on the browser console. (The stack snippet in the question makes the console more visible, but you can always view the console in your own browser's tools. This should always be an immediate debugging step.)

First, this is immediately producing an error:

for (var count = 0; count &lt;= y; count++) {
  var z = a[count];
  var x = z.addEventListener(&quot;click&quot;, Action);
}

Because of the &lt;= used in the loop condition, the last iteration of the loop exceeds the length of the array. Array indexes start at 0 and end just before the length of the array. Change the comparison to &lt;:

for (var count = 0; count &lt; y; count++) {
  var z = a[count];
  var x = z.addEventListener(&quot;click&quot;, Action);
}

Second, z isn't what you think it is here:

function Action() {
  var displayer = z.target.value;
  alert(&quot;Button value :&quot; + displayer);
}

At this point z is the last value from the loop above. Which is just outside of the array. So every event handler is trying to use the same (non-existant) element.

Fortunately, you don't need z from the loop above. The system passes an event object to click handlers, and you can use that object. For example, here I call that object e:

function Action(e) {
  var displayer = e.target.value;
  alert(&quot;Button value :&quot; + displayer);
}

Putting them both together:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

const a = document.querySelectorAll(&quot;.drum&quot;);
var y = a.length;
for (var count = 0; count &lt; y; count++) {
  var z = a[count];
  var x = z.addEventListener(&quot;click&quot;, Action);
}

function Action(e) {
  var displayer = e.target.value;
  alert(&quot;Button value :&quot; + displayer);
}

<!-- language: lang-css -->

body {
  text-align: center;
  background-color: #283149;
}

h1 {
  font-size: 5rem;
  color: #DBEDF3;
  font-family: &quot;Arvo&quot;, cursive;
  text-shadow: 3px 0 #DA0463;

}

footer {
  margin-top: 100px;
  color: #DBEDF3;
  font-family: sans-serif;
}

.pressed {
  box-shadow: 0 3px 4px 0 #DBEDF3;
  opacity: 0.5;
}

.inp{
  width: 346px;
  border-radius: 5px;
  height: 40px;
  text-align: right;
}


.red {
  color: red;
}

.drum {
  outline: none;
  border: 5px solid #404B69;
  font-size: 50px;
  font-family: &#39;Arvo&#39;, cursive;
  font-weight: 100;
  color: #DA0463;
  text-shadow: 3px 0 #DBEDF3;
  border-radius: 15px;
  display: inline-block;
  width: 80px;
  height: 80px;
  text-align: center;
  margin: 5px;
  background-color: white;
}

<!-- language: lang-html -->

&lt;h1 id=&quot;title&quot;&gt;My Calculator ✌&#127996;&lt;/h1&gt;
&lt;div &gt;
  &lt;button class=&quot;drum&quot; value=&quot;1&quot;&gt;1&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;2&quot;&gt;2&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;3&quot;&gt;3&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;+&quot;&gt;+&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;set&quot;&gt;
  &lt;button class=&quot;drum&quot; value=&quot;4&quot;&gt;4&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;5&quot;&gt;5&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;6&quot;&gt;6&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;-&quot;&gt;-&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;set&quot;&gt;
  &lt;button class=&quot;drum&quot; value=&quot;7&quot;&gt;7&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;8&quot;&gt;8&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;9&quot;&gt;9&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;*&quot;&gt;x&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;set&quot;&gt;
  &lt;button class=&quot;drum&quot; value=&quot;0&quot;&gt;0&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;/&quot;&gt;➗&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;.&quot;&gt;.&lt;/button&gt;
  &lt;button class=&quot;drum&quot; value=&quot;=&quot;&gt;=&lt;/button&gt;
&lt;/div&gt;
&lt;footer&gt;
  Made with ❤️ in Nigeria.
&lt;/footer&gt;

<!-- end snippet -->

答案2

得分: 0

这里是您的代码的翻译部分:

const a = document.getElementsByClassName("drum");

for (var count = 0; count < a.length; count++) {
  addAlertToButtonElement(a[count]);
}

function addAlertToButtonElement(z) {
  z.addEventListener("click", () => {
    alert("按钮值:" + z.value);
  });
}

一些观察:

  • getElementsByClassName("classHere") 返回一个HTML元素集合,这是您要添加事件监听器的类型。
  • querySelectorAll(".class") 返回一个NodeList,有些不同。

在您的动作函数中,您引用了 "z",而 "z" 是一个在循环范围内的变量。此外,在这种情况下,您需要移除目标(例如 z.value 而不是 z.target.value),这会在初始化时逐个触发所有警报,然后不再触发它们。而且,您因为循环从0到(包括)数组的长度(n+1)而导致索引越界的问题。

英文:

Here's a quick refactor of your fiddle that works:

const a = document.getElementsByClassName(&quot;drum&quot;);

for (var count = 0; count &lt; a.length; count++) {
  addAlertToButtonElement(a[count]);
}



function addAlertToButtonElement(z) {
	z.addEventListener(&quot;click&quot;, () =&gt; {
  	alert(&quot;Button value :&quot; + z.value);
  });
}

Some observations:

  • getElementsByClassName("classHere") returns a collection of HTML elements -> this is the type on which you want to add the event listener to
  • querySelectorAll(".class") returns a NodeList which is a bit different

You were referencing "z" in your Action function while z was a for-loop scoped variable. Also, in that case you needed to remove the target (e.g. z.value not z.target.value) which would fire all the alerts one by one at init and then it wouldn't fire them anymore.
And you were getting an index out of bounds problem because you were looping from 0 to (including) the length (n+1) size of the array.

huangapple
  • 本文由 发表于 2023年7月10日 19:22:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定