有没有办法用jQuery包装以下文本节点?

时间:2013-04-03 03:27:48

标签: jquery

我有以下内容:

HTML:

<div class="WishListMember_LoginMergeCode">
Killer Idea
<br>
Outsource Course
<br>
App Development
<br>
</div>

我想把它变成这个:

<div class="WishListMember_LoginMergeCode">
<a href="link1">Killer Idea</a>
<br>
<a href="link2">Outsource Course</a>
<br>
<a href="link3">App Development</a>
<br>
</div>

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

$('div.WishListMember_LoginMergeCode').contents().filter(function(){
   return this.nodeType === 3;
}).wrap('<a href="#"></a>');

http://jsfiddle.net/wjS6F/

更新

$('div').contents().filter(function() {
   return this.nodeType === 3;
}).wrap(function(index) {
   return '<a href="link'+ ++index +'"></a>';
});

http://jsfiddle.net/8P2gf/