2009-06-18 47 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