Javascript 如何在jQuery中删除段落中不需要的元素
我正在解析网页中的html内容 我需要将此段转换为:Javascript 如何在jQuery中删除段落中不需要的元素,javascript,jquery,html,Javascript,Jquery,Html,我正在解析网页中的html内容 我需要将此段转换为: <p>Bryan Cranston was a guest on the third episode of <a href="/wiki/ALF%27s_Hit_Talk_Show" title="ALF's Hit Talk Show">ALF's Hit Talk Show</a>. Bryan Cranston is an American actor best known for his roles
<p>Bryan Cranston was a guest on the third episode of <a href="/wiki/ALF%27s_Hit_Talk_Show" title="ALF's Hit Talk Show">ALF's Hit Talk Show</a>.
Bryan Cranston is an American actor best known for his roles as Walter White in <i>Breaking Bad</i>, for which he won three consecutive Outstanding Lead Actor in a Drama Series Emmy Awards; and as Hal, the father in <i>Malcolm in the Middle</i>.</p>
布莱恩·克兰斯顿是《哈利波特》第三集的嘉宾。
布莱恩·克兰斯顿是一名美国演员,因在《坏小子》中扮演沃尔特·怀特而闻名,并因此连续三次获得艾美奖戏剧系列中的杰出男主角;而哈尔,马尔科姆的父亲在中间。
为此:
<p>Bryan Cranston was a guest on the third episode of ALF's Hit Talk Show.
Bryan Cranston is an American actor best known for his roles as Walter White in <i>Breaking Bad</i>, for which he won three consecutive Outstanding Lead Actor in a Drama Series Emmy Awards; and as Hal, the father in <i>Malcolm in the Middle</i>.</p>
布莱恩·克兰斯顿是阿尔夫热门脱口秀节目第三集的嘉宾。
布莱恩·克兰斯顿是一名美国演员,因在《坏小子》中扮演沃尔特·怀特而闻名,并因此连续三次获得艾美奖戏剧系列中的杰出男主角;而哈尔,马尔科姆的父亲在中间。
如您所见,我只是保留I元素,并删除任何其他元素
我被困住了,这就是我到目前为止所做的:
提前谢谢 我不知道您为什么要使用each,但我认为这是必要的,然后您可以使用方法
.unwrap()
删除链接标记,但保留内部HTML。尝试以下操作:
$('p').each(function(){
$(this).find('a').contents().unwrap();
});
或者更容易
$('p a').contents().unwrap();
或
$('pa').contents().unwrap()
应该是dosure,但在示例中OP使用了each,我保留了它,我添加了这个解决方案,感谢@ArunPJohny的提示
$( 'p *' ).not( 'i' ).contents().unwrap();
$('p *').not( 'i' ).each(function(){
$( this ).replaceWith(this.childNodes);
});