Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
遍历JQuery对象并替换文本_Jquery - Fatal编程技术网

遍历JQuery对象并替换文本

遍历JQuery对象并替换文本,jquery,Jquery,假设我有下面的html div,我想复制它的子对象,在这个过程中更改每个跨度的值 <div><span class="foo">[Name]</span> <span class="bar">[Name]</span></div> 然后像中一样克隆 var clone = $div.children().clone(); 然后如何遍历克隆并更改每个跨度的值 clone.find("span").text("new val

假设我有下面的html div,我想复制它的子对象,在这个过程中更改每个跨度的值

<div><span class="foo">[Name]</span> <span class="bar">[Name]</span></div>
然后像中一样克隆

var clone = $div.children().clone();
然后如何遍历克隆并更改每个跨度的值

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

或者其他很多方式

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


或者很多其他的方式。

在我的脑海里

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

这没有经过任何方式的测试,但我希望你能理解我的想法。

从我的头脑中

var clone = $div.children().clone().each(function(){
  var textVale = $(this).text();
  //process the text;
  $(this).text(textValue);
 }
);
这没有经过任何方式的测试,但我希望你能明白这一点。

我认为你可以做到:

clone = $div.clone();
clone.children.each(function(i) {
  this.text("replacement text");
});
我现在不能测试它,但手册页是。

我想你可以做:

clone = $div.clone();
clone.children.each(function(i) {
  this.text("replacement text");
});
我现在不能测试它,但是手册页是