简体   繁体   中英

Jquery - Convert contents of html element to another element

I want to convert many elements of the same class into some other element. Let me explain...

I have multiple instances of this this HTML on my page:

<td>FirstName LastName <span class="fortipsy">Some long text .. Lorem ipsum dolor sit   amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat</span></td>

On page load I want to take all the span tags and convert each of them into this:

<td>FirstName LastName <a href="#" class="tipsy" title="Some long text .. Lorem ipsum    dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat"><img src="tipsy.gif" border="0"></a></td>

In short I want convert these elements into tipsy on page load. Any suggestions or leads? Thanks.

jsFiddle Demo

$(".fortipsy").each(function () {
    var txt = $(this).text();
    $(this).parent().append("<a href='#' class='tipsy' title='" + txt + "'><img src='tipsy.gif' border='0'></a>")
    $(this).remove();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM