使用jquery删除父span标记?

时间:2013-09-17 09:46:23

标签: javascript jquery html

我想从下面的代码中使用Jquery删除两个span标记 因为div是span的子级,所以在w3validators中会出错:

    <td class="col-1 col-first">
    **<span>
    <span>**
    <div class="user-picture">
    <a href="/users/shoprite-mp" title="View user profile.">
    <img typeof="foaf:Image" src="http://locationbasedmedia.co.za/sites/default/files/styles/thumbnail/public/pictures/picture-580-1379344365.jpg" alt="Shoprite Mp's picture" title="Shoprite Mp's picture"/>
    </a>
    </div>
    **</span>
    </span>**
    </td>

2 个答案:

答案 0 :(得分:3)

使用

$(".user-picture").unwrap();
$(".user-picture").unwrap();

使用两次展开来删除两个父跨度并保留内部结构。

答案 1 :(得分:0)

另一种解决方案是:

$('td').click(function () {
    var content = $('.user-picture', this).detach();
    $(this).append(content);
    $('span', this).remove()l
});

unwrap非常适合您的场景,但有时您想要将.user图片移动到不仅仅是2个级别,detach有助于保持对象(包括它的属性和事件完好无损。

相关问题