zoukankan      html  css  js  c++  java
  • javascript删除节点的兼容问题。

    一个删除节点的JS,在IE中删除最后一个节点没有问题,但在firefox中每次点了删除后不会删除,再点一次才能删除。

    <html>
     <head>
      <title>删除元素 Javascript RemoveChild</title>
      <script type="text/javascript">
       function delNode(){
        var para = document.getElementById("delUl");
        var paraLen = para.getElementsByTagName("li").length;
        if(paraLen>0)
        {
            para.removeChild(para.lastChild);

        }else{
         alert("没有了!");
        }
       }
      </script>
     </head>
     <body>
      <ul id="delUl">
       <li>list1</li>
       <li>list2</li>
       <li>list3</li>
       <li>list4</li>
       <li>list5</li>
      </ul>
      <input type="button" value="Delete" onclick="delNode();" />
     </body>
    </html>

    改为:

    function delNode(){
        var para = document.getElementById("delUl");
        var paraLen = para.getElementsByTagName("li").length;
        if(paraLen>0)
        {
            //while用于支持firefox,由于Firefox认为这里的lastChild是文本节点,直接removeChild(para.lastChild)不起作用
         while(para.lastChild && para.lastChild.nodeType == 3)
         {
          para.removeChild(para.lastChild);
         }

         para.removeChild(para.lastChild);
         
         //另一种更简便的方法:lastElementChild只支持Firefox
         //para.removeChild(para.lastElementChild ? para.lastElementChild : para.lastChild);

        }else{
         alert("没有了!");

         para.removeChild(para.lastChild);

        }

    这样IE和Firefox效果一样了!

  • 相关阅读:
    数据库 | 建表常用语句
    心得 | 撰写项目申报书
    工具 | 时间转化
    SpringBoot | 启动异常 | 显示bulid success 无 error信息
    120. 三角形最小路径和
    63. 不同路径 II
    SpringBoot | Velocity template
    SpringBoot | quartz | @DisallowConcurrentExecution
    SpringBoot | Hibernate @Transient 注解
    Java | 基础归纳 | 静态方法与实例方法的区别
  • 原文地址:https://www.cnblogs.com/load/p/2267205.html
Copyright © 2011-2022 走看看