我正在从网页解析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 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元素,并删除任何其他元素。
我被困住了,这是我到目前为止所做的: http://jsfiddle.net/JyxL4/1/
提前致谢!
答案 0 :(得分:2)
我不知道你为什么要使用每个,但我认为是必要的,那么你可以使用方法.unwrap()
删除链接标记,但保留内部HTML。
试试这个:
$('p').each(function(){
$(this).find('a').contents().unwrap();
});
或更容易
$('p a').contents().unwrap();
答案 1 :(得分:1)
$( 'p *' ).not( 'i' ).contents().unwrap();
或
$('p *').not( 'i' ).each(function(){
$( this ).replaceWith(this.childNodes);
});