删除<a> within container</a>

时间:2011-10-15 20:26:31

标签: jquery

我有这个:

<ul>
   <li><a href="http://link.com">Link 1</a></li>
   <li class="remove-me"><a>Link 2</a></li>
   <li><a href="http://link.com">Link 3</a></li>
</ul>

我想这样:

<ul>
   <li><a href="http://link.com">Link 1</a></li>
   <li class="remove-me">Link 2</li>
   <li><a href="http://link.com">Link 3</a></li>
</ul>

如何用jquery实现这一点?我想$('.remove-me').removeAttr('a');但不是吗?

7 个答案:

答案 0 :(得分:2)

这将从a中删除所有HTML标记(包括li):

$('li.remove-me').text(function(i, text) { return text; });

如果您只想删除a代码:

 $('li.remove-me a').replaceWith(function() { return $(this).contents(); });

答案 1 :(得分:1)

试试这个:

$('.remove-me').each(function(){
    $(this).text($(this).text());
});

答案 2 :(得分:0)

尝试$('.remove-me a').remove()

编辑:我发现您要保留a元素的内容。在这种情况下,使用replaceWith()应该有效:

$('.remove-me a').replaceWith( function() {
    return $(this).contents();
} );

答案 3 :(得分:0)

首先想一想,假设您要保留li

中的文字
$('li.remove-me').each(
    function(){
        $(this).text($(this).find('a').text()).find('a').remove();
    });

JS Fiddle demo

或者,或者:

var text = "";

$('li.remove-me').each(
    function(){
        text = $(this).text();
        $(this).empty().text(text);
    });

JS Fiddle demo

答案 4 :(得分:0)

这将仅删除a标记并保留其内容:

$(".remove-me a").replaceWith(function(i, content) { return content; })

答案 5 :(得分:0)

这将有效: $('.remove-me a').replaceWith($('.remove-me a').text());

答案 6 :(得分:-1)

尝试: 的 1

var val= $('.remove-me').text();
$('.remove-me').empty().text(val);

2。

var val= $('.remove-me').text();
$('.remove-me').html(val);

3。

$('.remove-me').each(
    function(){
        $(this).html($("a", this).first().text());
});

See the demo (contains all code)