我有这个:
<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');
但不是吗?
答案 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)
编辑:我发现您要保留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();
});
或者,或者:
var text = "";
$('li.remove-me').each(
function(){
text = $(this).text();
$(this).empty().text(text);
});
答案 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());
});