我有一个小问题,在我的菜单中有一些没有hrefs的链接(比如我的空链接).如果它没有href,我想删除但保留文本.
这就是我得到的:
$('.mainmenu a, .mainmenu a *').each(function(){
var href = $(this).attr('href');
if(!href)
{
console.log($(this).html());
//Remove <a> from this element, how?
}
});
Run Code Online (Sandbox Code Playgroud)
请帮忙?
然后你需要解开内容
$('.mainmenu a:not([href])').contents().unwrap()
Run Code Online (Sandbox Code Playgroud)
:不过滤掉没有href标签的元素
.contents()返回一个包含内容的jQuery对象 - 即文本
.unwrap()删除它周围的锚标记