-1

我有以下内容:

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>

如何做到这一点?

4

1 回答 1

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/

于 2013-04-03T03:32:56.610 回答