【问题标题】:replaceChild jQuery equivalentreplaceChild jQuery 等价物
【发布时间】:2013-09-13 04:37:47
【问题描述】:

我正在尝试用我制作的新元素替换当前的 dom 元素。

我实现了以下行

$(this).parent().replaceChild(newElem,this);

但它给出了错误提示 $(this).parent().replaceChild is not a function 因为 replaceChild 不是 jQuery 函数,在 jQuery 中什么是等效的?

【问题讨论】:

    标签: javascript jquery jquery-plugins


    【解决方案1】:

    如果你想要替换里面:

    $(this).empty().append('<p>replacement</p>')
    

    【讨论】:

      【解决方案2】:

      如果你想使用纯 jQuery,你可以使用 replaceWith 方法

      $(this).replaceWith(newElem);
      

      你可以参考这个链接:

      http://api.jquery.com/replaceWith/

      【讨论】:

        【解决方案3】:

        你可以试试这个

        $(this).parent().get().replaceChild(newElem,this);
        

        如果$(this).parent() 在页面上是唯一的。 (.get() 给你 DOM 对象)

        如果你想使用纯jQuery,你可以使用replaceWith方法

        $(this).replaceWith(newElem);
        

        【讨论】:

        • 它替换了父元素。
        • 如果只想替换current元素..去掉.parent
        【解决方案4】:

        如果要替换父元素中的元素,请执行以下操作。

        这是替换元素的所有子元素的方法:

        $(this).children().replaceWith(<div>hello world</div>)
        

        这是用索引替换元素的特定子元素的方法:

        $($(this).children()[3]).replaceWith(<div>hello world</div>)
        

        【讨论】:

          【解决方案5】:

          试试replacewith()

          $(this).parent().replaceWith(newElem);
          

          【讨论】:

          • 它替换了父元素。
          【解决方案6】:

          对于任何寻找普通 JS 方法的人,请使用 replaceWith

          this.replaceWith(newElem);
          

          【讨论】:

            猜你喜欢
            • 2016-08-30
            • 2011-10-15
            • 1970-01-01
            • 1970-01-01
            • 2013-03-02
            • 2011-08-03
            • 2011-05-11
            • 2013-09-11
            • 2010-11-07
            相关资源
            最近更新 更多