【问题标题】:JavaScript :- remove and add element [duplicate]JavaScript:-删除和添加元素[重复]
【发布时间】:2019-03-23 08:17:45
【问题描述】:
<div id="div-02">Here is div-02</div>
var el = document.getElementById('div-02');
el.remove(); // Removes the div with the 'div-02' id

setTimeout(() => {
el.add?... },5000)

我想删除该元素 5 秒钟,并使其与之前相同(在删除之前)。

【问题讨论】:

  • 也许可以试试display: none
  • 只是要求澄清一下,要删除5秒再恢复吗?如果是这样,您可以隐藏和显示
  • 你好,可以用jquery还是纯js?
  • 我正在做 Angular 项目,所以可能是纯 js,但 hide and show 解决了我的问题,我走错了方向

标签: javascript


【解决方案1】:

您可以将元素放入临时变量中,以便在时间结束后附加该元素:

var temp = document.getElementById('div-02');
var el = document.getElementById('div-02');
el.remove(); // Removes the div with the 'div-02' id
setTimeout(() => {
  document.body.append(temp); 
},5000)
&lt;div id="div-02"&gt;Here is div-02&lt;/div&gt;

但理想的解决方案是使用元素的样式属性隐藏/显示:

var el = document.getElementById('div-02');
el.style.display = 'none';
setTimeout(() => {
  el.style.display = 'block';
},5000)
&lt;div id="div-02"&gt;Here is div-02&lt;/div&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2012-10-21
    • 2020-06-18
    • 2015-10-04
    相关资源
    最近更新 更多