简体   繁体   English

如何使用JavaScript清除“ innerHTML”?

[英]How can I clear a “innerHTML” with Javascript?

I have this HTML: 我有这个HTML:

<div id = "options"></div>

I access to this div trough this Javascript: 我可以通过以下Javascript访问该div:

var test = "hola"; 
document.getElementById('options').innerHTML= 
<p>Test</p> <li>${test}</li>

And I have this button: 我有这个按钮:

function click({
  document.getElementById("Click").addEventListener("click", function(){
  });
};
click();

I need that when that function called "click" is executed automatically that <li> can clean that value of the variable: "test" and it is seen empty. 我需要当自动执行名为“ click”的函数时, <li>可以清除变量的值:“ test”,然后将其视为空。

How can I do it? 我该怎么做? Thank you 谢谢

el.innerHTML is the property which keeps track of HTML text values shown inside the element. el.innerHTML是用于跟踪元素内显示的HTML文本值的属性。 You can set that to empty whenever you want to clear it. 您可以随时将其设置为空。

Try this sample - https://jsitor.com/J0gXikZmV 试试这个示例-https: //jsitor.com/J0gXikZmV

<div id = "options"></div>

var test = "hola"; 
let el = document.getElementById("options");

el.innerHTML = "<p>Test</p> <li>${test}</li>";

el.addEventListener('click', () => {
  el.innerHTML = '';
})

Here it clear the html content of div 这里清除了div的html内容

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

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