简体   繁体   English

Jquery - 将html元素的内容转换为另一个元素

[英]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: 我的页面上有这个HTML的多个实例:

<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: 在页面加载时,我想获取所有span标记并将它们转换为:

<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 jsFiddle演示

$(".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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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