当前位置: 动力学知识库 > 问答 > 编程问答 >

Strip tags using jQuery?

问题描述:

HTML:

<table>

<tr>

<td>

<a href="#">link</a>

</td>

</tr>

</table>

I want to: FIND 'link' in 'a' and STRIP ALL parent tags except/until 'table'. I know its not the ideal way to do it, but sometimes you just can't modify the markup so it'd be great to know a jquery hack for this.

网友答案:
var a = $("a:contains('link')");
var table = a.closest('table');
a.insertBefore( table );        // Edit: See comments below
table.contents().remove();
table.append( a );

Edit: Calling remove() will also remove any event handlers that might be attached to the anchor element. To get around that, move the anchor outside of the table before removing its contents.

分享给朋友:
您可能感兴趣的文章:
随机阅读: