【问题标题】:Remove parent or child element on page load using jquery [duplicate]使用jquery在页面加载时删除父元素或子元素[重复]
【发布时间】:2014-05-23 03:56:35
【问题描述】:

我想在页面加载时删除围绕代码块的锚元素。

当页面加载时,我想要这段代码:

<h3 class="ms-standardheader ms-WPTitle" style="text-align: justify;">
<a accesskey="W" href="#">
<nobr>
<span>Employee Announcements</span>
<span id="WebPartCaptionWPQ3"></span>
</nobr>
</a>
</h3>

变成这段代码:

<h3 class="ms-standardheader ms-WPTitle" style="text-align: justify;">
<nobr>
<span>Employee Announcements</span>
<span id="WebPartCaptionWPQ3"></span>
</nobr>
</h3>

我曾尝试使用 unwrap 来完成这项工作,但我仍在苦苦挣扎,因为我想离开周围的 nobr 元素。

【问题讨论】:

标签: jquery dom element


【解决方案1】:

jQuery 有一个.unwrap() 函数,可以删除元素的父元素。

http://api.jquery.com/unwrap/

示例:

$(function() {
   $('#WebPartCaptionWPQ3').unwrap();
});

【讨论】:

    【解决方案2】:
    $(function() {
        var target = $('a[accesskey="W"]');
        var keep = target.children();
        target.replaceWith(keep);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2013-01-17
      • 2018-01-17
      • 2016-09-15
      • 2011-02-18
      • 2021-02-18
      相关资源
      最近更新 更多