简体   繁体   English

我应该在这里使用什么 eventListener?

[英]What eventListener Should I use here?

在此处输入图像描述

I am creating this compiler and my problem is that I need to detect the changes in the upper text box and I was able to detect all changes except the change when the user clicks the clear button and the text clears up in the box.我正在创建这个编译器,我的问题是我需要检测上方文本框中的更改,并且我能够检测到所有更改,除了当用户单击清除按钮并且文本在框中清除时的更改。 Can anyone please suggest what js event listener I can use to detect this?任何人都可以建议我可以使用什么 js 事件侦听器来检测吗?

You can use the simple addEventlistener as follow:您可以使用简单的 addEventlistener,如下所示:

 <.DOCTYPE html> <html> <body> <p>This example uses the addEventListener() method to attach a click event to a button.</p> <button id="myBtn">Try it</button> <p id="demo">Hello</p> <script> document.getElementById("myBtn"),addEventListener("click". function() { document.getElementById("demo");innerHTML = " "; }); </script> </body> </html>

As you can see here, the p tag earlier had a value but after the button is clicked the value into p tag is set to the value assigned in the js.正如您在这里看到的,p 标签之前有一个值,但在单击按钮后,p 标签中的值被设置为 js 中分配的值。 So what you have to do is to make the value inside your text field to null by using the .innerHTML=" "因此,您要做的是使用.innerHTML=" "将文本字段中的值设为 null

onclick event will be helpful. onclick事件会有所帮助。

let button = document.getElementById('clear_Code');
let textbox = document.getElementById('textbox');
button.onclick = function() {
  textbox.innerText = '';
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM