[英]how to make clickable buttons that add text into a div
i want to create a function that adds text into an empty div depending on what button is clicked 我想创建一个函数,根据单击的按钮将文本添加到一个空的div中
i'm making a calculator as one of my beginner javascript projects (i know i can just use inputs and the "button" tag but i feel this'll be more original and challenging). 我正在做一个计算器,这是我的初学者javascript项目之一(我知道我可以只使用输入和“ button”标签,但我觉得这会变得更原始且更具挑战性)。 i'm trying to add events to all the elements in the queryselector all nodelist, i feel my function is correct and i've tried all i can but the code doesn't seem to work, any help at all would be appreciated.
我正在尝试将事件添加到queryselector所有节点列表中的所有元素,我觉得我的功能是正确的,我已经尽力了,但是代码似乎没有用,任何帮助都将不胜感激。
i am trying to create a kind of like input div that reflects the value or text of the clicked list item 我正在尝试创建一种类似input div的方法,以反映单击列表项的值或文本
let digits = document.querySelectorAll("digit"); let add = document.getElementById("camper"); for (let i = 0; i < digits.length; i++) { digits[i].addEventListener("click", function() { add.innerHTML += digits[i].innerHTML }); digits[i].value = [i]; };
<div class="container"> <div class="camper"> <h3 id="insert"></h3> </div> <div class="right-box"> <ul> <li> <h3 id="equal">=</h3> </li> <li> <h3 id="plus">+</h3> </li> <li> <h3 id="minus">-</h3> </li> <li> <h3 id="multiply">x</h3> </li> <li> <h3 id="divide">\\</h3> </li> </ul> </div> <div class="write-box"> <ul> <li class="digit"> <h3>1</h3> </li> <li class="digit"> <h3>2</h3> </li> <li class="digit"> <h3>3</h3> </li> <li class="digit"> <h3>4</h3> </li> <li class="digit"> <h3>5</h3> </li> <li class="digit"> <h3>6</h3> </li> <li class="digit"> <h3>7</h3> </li> <li class="digit"> <h3>8</h3> </li> <li class="digit"> <h3>9</h3> </li> <li class="digit"> <h3>0</h3> </li> <li> <h3>clear</h3> </li> <li> <h3>bkspc</h3> </li> </ul> </div> </div>
Typos 错别字
id="camper"
and not class="camper"
if you want to use getElementById but you likely want insert
instead of camper
id="camper"
而不是class="camper"
,但您可能希望insert
而不是camper
querySelectorAll(".digit")
- you are missing the dot in the class selector querySelectorAll(".digit")
-您在类选择器中缺少点 Logic 逻辑
this.innerText
to get the content of the digit this.innerText
获取数字的内容 let digits = document.querySelectorAll(".digit"); let add = document.getElementById("insert"); for (let i = 0; i < digits.length; i++) { digits[i].addEventListener("click", function() { add.innerHTML += this.innerText; }); };
<div class="container"> <div class="camper"> <h3 id="insert"></h3> </div> <div class="right-box"> <ul> <li> <h3 id="equal">=</h3> </li> <li> <h3 id="plus">+</h3> </li> <li> <h3 id="minus">-</h3> </li> <li> <h3 id="multiply">x</h3> </li> <li> <h3 id="divide">\\</h3> </li> </ul> </div> <div class="write-box"> <ul> <li class="digit"> <h3>1</h3> </li> <li class="digit"> <h3>2</h3> </li> <li class="digit"> <h3>3</h3> </li> <li class="digit"> <h3>4</h3> </li> <li class="digit"> <h3>5</h3> </li> <li class="digit"> <h3>6</h3> </li> <li class="digit"> <h3>7</h3> </li> <li class="digit"> <h3>8</h3> </li> <li class="digit"> <h3>9</h3> </li> <li class="digit"> <h3>0</h3> </li> <li> <h3>clear</h3> </li> <li> <h3>bkspc</h3> </li> </ul> </div> </div>
Here is a more elegant way - if you are doing homework, you might want to do more coding before looking :) 这是一种更优雅的方法-如果您正在做作业,则可能需要在看之前进行更多的编码:)
let add = document.getElementById("insert"); document.getElementById("container").addEventListener("click",function(e) { const tgt = e.target; if (tgt.tagName==="H3") { const text = tgt.innerText; const cn = tgt.parentNode.className; if (cn === "digit" || cn === "oper") add.innerText += text; else if (cn==="action") { if (text === "clear") { add.innerText = ""; } else if (text === "bkspc") { add.innerText = add.innerText.slice(0,-1); } } } })
<div id="container"> <div class="camper"> <h2 id="insert"></h2> </div> <div class="right-box"> <ul> <li class="oper"> <h3 id="equal">=</h3> </li> <li class="oper"> <h3 id="plus">+</h3> </li> <li class="oper"> <h3 id="minus">-</h3> </li> <li class="oper"> <h3 id="multiply">x</h3> </li> <li class="oper"> <h3 id="divide">\\</h3> </li> </ul> </div> <div class="write-box"> <ul> <li class="digit"> <h3>1</h3> </li> <li class="digit"> <h3>2</h3> </li> <li class="digit"> <h3>3</h3> </li> <li class="digit"> <h3>4</h3> </li> <li class="digit"> <h3>5</h3> </li> <li class="digit"> <h3>6</h3> </li> <li class="digit"> <h3>7</h3> </li> <li class="digit"> <h3>8</h3> </li> <li class="digit"> <h3>9</h3> </li> <li class="digit"> <h3>0</h3> </li> <li class="action"> <h3>clear</h3> </li> <li class="action"> <h3>bkspc</h3> </li> </ul> </div> </div>
You are losing the scope of digits
inside the event listener. 您正在丢失事件侦听器中的
digits
范围。 You need to bind digits
to be the scope of this
in the event listener like so: 您需要在事件侦听器中将
digits
绑定this
范围,如下所示:
let digits = document.querySelectorAll("digit");
let add = document.getElementById("camper");
for (let i = 0; i < digits.length; i++) {
digits[i].addEventListener("click", function() {
add.innerHTML += this.innerHTML
}).bind(digits[i]);
digits[i].value = [i];
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.