简体   繁体   English

从DOM中删除节点

[英]Remove Node from DOM

Noob question alert! 新手问题提示! I am working on a basic clock that updates every second, but its not updating because I get Uncaught Error: NOT_FOUND_ERR: DOM Exception 8. Basically i just need to change the removeChild() at the end of the function to something else, but I don't know what to change it to. 我正在使用每秒更新一次的基本时钟,但由于我收到未捕获的错误而未更新:NOT_FOUND_ERR:DOM异常8。基本上,我只需要将函数末尾的removeChild()更改为其他内容,但是我不知道将其更改为什么。 I want to remove the <p class="time"> that is being created. 我想删除正在创建的<p class="time">

function spitTime() {
    var clock = new Date();
    var hours = clock.getHours();
    var minutes = clock.getMinutes();
    var seconds = clock.getSeconds();

    if (minutes <= 9) {
        minutes = "0" + minutes;
    }

    if (seconds <= 9) {
        seconds = "0" + seconds;
    }

    var displayTime = hours + ":" + minutes + ":" + seconds;
    var newTimeElem = document.createElement("p");
    newTimeElem.className = "time";
    var timeText = document.createTextNode(displayTime);
    newTimeElem.appendChild(timeText);
    document.getElementById("timePlace").appendChild(newTimeElem);
    document.getElementById("timePlace").removeChild();
}

relevant HTML: 相关的HTML:

<div class="clock">
    <span id="timePlace"></span>
</div>

According to your question, you don't want to remove a child, you want to change the text of an existing element. 根据您的问题,您不想删除子项,而是想更改现有元素的文本。

For this, you can use textContent : 为此,您可以使用textContent

document.getElementById( 'timePlace' ).textContent = displayTime;

It is to be noted that this isn't cross browser. 请注意,这不是跨浏览器。 IE8 and below work with innerText instead. IE8及以下版本改为使用innerText A quick hack to use is this way: 可以使用以下快速技巧:

var timePlace = document.getElementById( 'timePlace' );
if ( 'textContent' in timePlace ) {
    timePlace.textContent = displayTime;
}
else {
    timePlace.innerText = displayTime;
}

However, it is a hack. 但是,这是一个hack。 It will work nicely for your case, but don't rely on this. 它会很好地适合您的情况,但不要依赖于此。

If you really want cross browser compatibility, you're using jQuery by the way (or any other library like this). 如果您确实希望跨浏览器兼容,则可以使用jQuery(或其他类似的库)。 jQuery way to change text: jQuery更改文本的方式:

$( '#timePlace' ).text( displayTime );

It uses nodeValue , the only real cross-browser way to deal with text. 它使用nodeValue ,这是处理文本的唯一真正的跨浏览器方式。 It has to do this recursively though, because it is only available on textNodes . 但是,它必须递归地执行此操作,因为它仅在textNodes可用。 It is equivalent to using data (as @amnotiam does in the comments of your question). 这等效于使用data (就像@amnotiam在问题注释中所做的那样)。

You are not specifying a child node to be removed. 您没有指定要删除的子节点。

Node.removeChild Node.removeChild

var oldChild = element.removeChild(child);
element.removeChild(child);
  • child is the child node to be removed from the DOM. child是要从DOM中删除的子节点。
  • element is the parent node of child. element是child的父节点。
  • oldChild holds a reference to the removed child node. oldChild拥有对已删除子节点的引用。 oldChild == child oldChild ==孩子

If you want to remove the timePlace element itself: 如果要删除timePlace元素本身:

document
    .getElementById("timePlace")
    .parentNode
    .removeChild(document.getElementById('timePlace'));

Assuming this is the intent, this works by by finding the relevant element/node ( getElementById() ), moving to the parentNode of that element and then removing the element from the parent (since removeChild() both expects a DOM node to be passed to it, and removes a child node of the current element/node). 假设这是目的,则可以通过找到相关元素/节点( getElementById() ),移至该元素的parentNode ,然后从父元素中删除该元素(因为removeChild()都希望传递DOM节点)来进行工作到它, 删除当前元素/节点的子节点)。


Updated according to the comment from OP (below), simply use: 根据OP的评论(如下)进行了更新,只需使用:

newTimeElem.parentNode.removeChild(newTimeElem);

References: 参考文献:

    <script type="text/javascript"> 
        function spitTime() {
            var clock = new Date();
            var hours = clock.getHours();
            var minutes = clock.getMinutes();
            var seconds = clock.getSeconds();

            if (minutes <= 9) {
                minutes = "0" + minutes;
            }

            if (seconds <= 9) {
                seconds = "0" + seconds;
            }

            var displayTime = hours + ":" + minutes + ":" + seconds;
            var newTimeElem = document.createElement("p");
            newTimeElem.className = "time";
            var timeText = document.createTextNode(displayTime);
            newTimeElem.appendChild(timeText);
            $('#timePlace').children('.time').remove();
            document.getElementById("timePlace").appendChild(newTimeElem);
            setTimeout('spitTime()', 100);
        }
        $(document).ready(function(){
            setTimeout('spitTime()', 100);
        });
    </script>
</head>
<body>
    <div class="clock">
        <span id="timePlace"></span>
    </div>
</body>

Link: http://lib-eguide.nus.edu.sg/test1.php 链接: http//lib-eguide.nus.edu.sg/test1.php

while (timePlace.firstChild) {
    timePlace.removeChild(timePlace.firstChild)
}

Is what I was going for, though it might be better just to edit the content, this works for me very well. 这就是我想要的,尽管仅编辑内容可能会更好,但这对我来说很好。

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

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