【问题标题】:Uncaught NotFoundError: Failed to execute 'replaceChild' on 'Node': The node to be replaced is not a child of this nodeUncaught NotFoundError: Failed to execute 'replaceChild' on 'Node': 要替换的节点不是该节点的子节点
【发布时间】:2020-11-23 23:15:32
【问题描述】:

我在我的代码中找不到问题,有人可以帮忙解决这个问题吗? 这是我的源代码

<button onclick="myfunction()" class="button" id="btw1" > Voir numéro   </button>

<script type=text/javascript> 
            function myfunction(){
           
            var bouton= document.getElementById('btw1');
           
             var parent= document.body;

            var nvbtw = document.createElement('btw2');
            nvbtw.id='btw2';
            nvbtw.innerHTML='<button  class="button" id="btw2" > (+33 00 00 00 00 00)   </button>';
            
            parent.replaceChild(nvbtw,bouton);
}</script>

我想要做的是一个隐藏的按钮编号,就像当你点击它时它会显示数字。

【问题讨论】:

    标签: javascript


    【解决方案1】:

    错误消息告诉您bouton 不是document.body直接 子级。代替var parent = document.body,使用:

    var parent = bouton.parentElement; // or `.parentNode`
    

    附注:如果您的目标是现代浏览器,您可能会发现replaceWith 更易于使用:

    bouton.replaceWith(nvbtw);
    

    (它也可以为旧版浏览器填充。)

    【讨论】:

      猜你喜欢
      • 2014-03-04
      • 2019-03-03
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 2022-11-12
      • 2018-03-25
      相关资源
      最近更新 更多