2010-05-21 80 views
1

我正在尝试创建一个jQuery的安装程序,其中<i>的所有实例都更改为<em>。这是很容易与做:用jquery替换元素名称,同时保留属性

$("i").each(function(){ 
    $(this).replaceWith($('<em>' + this.innerHTML + '</em>')); 
}); 

但是我有麻烦搞清楚是如何改变一切<i>标签,但保留每一个个体的人的属性。所以如果我有<i style="background-color: #333;" alt="Example" title="Example">Example Text</i>我希望它变成<em style="background-color: #333;" alt="Example" title="Example">Example Text</em>

感谢您的帮助!

回答

0

它是简单

$(本).attributes();将为您获取每个实例的i标签的所有属性,只需将它放入替换函数即可。

我不知道你怎么玩功能,但在我的知识代码将是这样的。

$("i").each(function(){ 
var attribute = $(this).attributes(); 
$(this).replaceWith($('<em'+attributes+'>' 

+ this.innerHTML + '')); });

希望它有帮助。

+0

jQuery没有*属性*方法... – James 2010-05-21 21:27:26

相关问题