Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to simplify my code using jquery?

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 :)

like image 830
Koerr Avatar asked Dec 30 '10 05:12

Koerr


People also ask

Does jQuery simplify JavaScript?

jQuery is a JavaScript library that helps to simplify and standardize interactions between JavaScript code and HTML elements.

How does jQuery simplify Web development?

jQuery promotes simplicityWith simple syntax and open coding standards, developers can shorten the time it takes to deploy an application or site. In addition, developers don't have to be experts in programming or Web design to create great styles for their sites.

What is $() in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements.


1 Answers

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

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

You can find a working example here.

like image 70
Arun P Johny Avatar answered Sep 22 '22 13:09

Arun P Johny