jquery克隆div然后到处查找并替换
编辑 我尝试了下面的两个建议,但它仍然不起作用...它克隆,但不增加...
function createNewSection() {
var cloneUp = $('.section-form').length;
var cloneCount = $('.section-form').length - 1;
var newSection = $('#section-0').clone(function() {
$(this).find('*').each(function() {
$(this).attr('id', $('#section-0').attr('id').replace(/section-0/,'section-'+cloneUp).replace(/section[_\[]0\]?/,'section_'+cloneUp));
$(this).attr('name', $('#section-0').attr('replace').replace(/section-0/,'section-'+cloneUp).replace(/section[_\[]0\]?/,'section_'+cloneUp));
});
});
$('#sections').append(newSection);
}
我试图替换特定字符串的所有实例并尝试了一些有很多方法可以做到这一点,但通常情况下,我会收到一条错误消息,指出 Replace 不是函数,$(this) 未定义,或缺少 : after argument...
例如,在以下 HTML 中:
<div id="section-1-add">
<div id="section-1">
<span class="section_1-span">Title</span>
<input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />
</div>
</div>
我想要替换所有实例:
section-1 with section-2
section_1 with section_2
section[1] with section_2
我已经尝试了本质上的许多变体同样的事情,但这是最近的尝试:
function createNewSection() {
var cloneUp = 2; //this is currently hardcoded, but will be dynamic... just for this example
var newSection = $('#section-1-add').clone(function() {
$(this).find('*').each(function() {
$(this).id=$(this).id.replace(/section-1/,'section-'+cloneUp).replace(/section[_\[]1\]?/,'section_'+cloneUp);
$(this).name=$(this).name.replace(/section-1/,'section-'+cloneUp).replace(/section[_\[]1\]?/,'section_'+cloneUp);
});
});
$('#sections').append(newSection);
}
在上面的代码中,该部分被克隆,但 id 或名称都没有改变......
EDIT
I tried both of the suggestions below, but it's still not working... It clones, but doesn't increment...
function createNewSection() {
var cloneUp = $('.section-form').length;
var cloneCount = $('.section-form').length - 1;
var newSection = $('#section-0').clone(function() {
$(this).find('*').each(function() {
$(this).attr('id', $('#section-0').attr('id').replace(/section-0/,'section-'+cloneUp).replace(/section[_\[]0\]?/,'section_'+cloneUp));
$(this).attr('name', $('#section-0').attr('replace').replace(/section-0/,'section-'+cloneUp).replace(/section[_\[]0\]?/,'section_'+cloneUp));
});
});
$('#sections').append(newSection);
}
I'm trying to replace all instances of a particular string and have tried a number of ways to do it, but more often than not, I get an error saying replace isn't a function, $(this) is undefined, or missing : after argument...
For example, in the following HTML:
<div id="section-1-add">
<div id="section-1">
<span class="section_1-span">Title</span>
<input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />
</div>
</div>
I want to replace all instances of:
section-1 with section-2
section_1 with section_2
section[1] with section_2
I've tried many variations of essentially the same thing, but here is the most recent attempt:
function createNewSection() {
var cloneUp = 2; //this is currently hardcoded, but will be dynamic... just for this example
var newSection = $('#section-1-add').clone(function() {
$(this).find('*').each(function() {
$(this).id=$(this).id.replace(/section-1/,'section-'+cloneUp).replace(/section[_\[]1\]?/,'section_'+cloneUp);
$(this).name=$(this).name.replace(/section-1/,'section-'+cloneUp).replace(/section[_\[]1\]?/,'section_'+cloneUp);
});
});
$('#sections').append(newSection);
}
In the code above, the section is cloned, but none of the ids or names change...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 .attr() 函数来获取和替换 id、class 值。例如:
You should use .attr() function to get and replace id, class value. For example:
下面的代码:
标识了一个 jQuery 对象,但看起来好像要修改实际的 DOM 对象。要获取该 DOM 对象,请尝试...
The following code:
identifies a jQuery Object, but it looks as if you want to modify the actual DOM object. To get that DOM object, try...