2010-05-21 77 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

相關問題