admin管理员组

文章数量:1405618

I want append <span></span> tag in my every <a> tag:

now:

<a href=#>aaa</a>
<a href=#>bbb</a>
<a href=#>ccc</a>

I want:

<a href=#><span>aaa</span></a>
<a href=#><span>bbb</span></a>
<a href=#><span>ccc</span></a>

now ,i using below codes to implement it:

$(function(){
    var buttons = $("a");
    var text=buttons.text();
    buttons.text("");
    buttons.prepend("<span>"+text+"</span>");
});

I think this codes is not good,how to simplify it?

thanks :)

I want append <span></span> tag in my every <a> tag:

now:

<a href=#>aaa</a>
<a href=#>bbb</a>
<a href=#>ccc</a>

I want:

<a href=#><span>aaa</span></a>
<a href=#><span>bbb</span></a>
<a href=#><span>ccc</span></a>

now ,i using below codes to implement it:

$(function(){
    var buttons = $("a");
    var text=buttons.text();
    buttons.text("");
    buttons.prepend("<span>"+text+"</span>");
});

I think this codes is not good,how to simplify it?

thanks :)

Share Improve this question asked Dec 30, 2010 at 5:20 KoerrKoerr 15.8k29 gold badges80 silver badges109 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

I think What you are looking for is the wrapinner function.

 $("a").wrapInner("<span></span>")

You can find a working example here.

本文标签: javascripthow to simplify my code using jqueryStack Overflow