2009-06-18 20 views
1

假設我有我要複製其兒童以下HTML DIV,改變的過程中遍歷一個jQuery對象和替換文本

<div><span class="foo">[Name]</span> <span class="bar">[Name]</span></div> 

使用jQuery每個跨度的價值,我可以得到的一個參考在

var clone = $div.children().clone(); 

「格」 由

var $div = $("div"); 

,然後克隆我哪有那麼遍歷克隆並更改每個跨度的值?

回答

3
clone.find("span").text("new value"); 

clone.find("span").each(function() { 
    $(this).text("new text"); 
}); 

clone.find("span.name").text("new name").siblings("span.bar").text("new bar") 

或許多其他的方式。

1
clone.find('span').each(function() { 
    $(this).text('Hahahha'); 
}); 
0

關閉我的頭頂......

var clone = $div.children().clone().each(function(){ 
    var textVale = $(this).text(); 
    //process the text; 
    $(this).text(textValue); 
} 
); 

這是不以任何方式進行測試,但我希望你的想法。

0

我認爲你可以這樣做:

clone = $div.clone(); 
clone.children.each(function(i) { 
    this.text("replacement text"); 
}); 

我現在不能測試,但該手冊是here