jquery克隆div然后到处查找并替换

发布于 2024-12-09 12:07:01 字数 1888 浏览 0 评论 0原文

编辑 我尝试了下面的两个建议,但它仍然不起作用...它克隆,但不增加...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本王不退位尔等都是臣 2024-12-16 12:07:01

您应该使用 .attr() 函数来获取和替换 id、class 值。例如:

$('#section-1').attr('id', $('#section-1').attr('id').replace(/section-1/g, 'section_1'));

You should use .attr() function to get and replace id, class value. For example:

$('#section-1').attr('id', $('#section-1').attr('id').replace(/section-1/g, 'section_1'));
心是晴朗的。 2024-12-16 12:07:01

下面的代码:

 $(this)

标识了一个 jQuery 对象,但看起来好像要修改实际的 DOM 对象。要获取该 DOM 对象,请尝试...

 $(this)[0].id = $(this)[0].id.replace(/*etc*/);

The following code:

 $(this)

identifies a jQuery Object, but it looks as if you want to modify the actual DOM object. To get that DOM object, try...

 $(this)[0].id = $(this)[0].id.replace(/*etc*/);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文