带有新 ID 的 jQuery .clone .insertAfter

发布于 2025-01-03 23:36:57 字数 314 浏览 4 评论 0原文

如何克隆 DIV,将其附加到 div 之后并为其指定新的标识符?

我有一个带有类 reflection 的 DIV,我希望克隆该 DIV 的每个实例,并将其插入到原始 DIV 之后,但带有类 reflected (没有类, reflection) 我怎样才能用 jQuery 完成这个任务?

这是我当前的小提琴,但它无法正常工作... http://jsfiddle.net/yUgn9/

How can I clone a DIV, append it right after the div and give it a new identifier?

I have a DIV with a class reflection, I want each instance of this DIV, cloned, and inserted right after the original, but with the class reflected (without the class, reflection) How can I accomplish this with jQuery?

Here's my current fiddle, but it doesn't work right... http://jsfiddle.net/yUgn9/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

柳絮泡泡 2025-01-10 23:36:57

将函数传递给 .after(),并让它返回更新后的克隆。

$('div.reflection').after(function() {
    return $(this).clone().toggleClass('reflection reflected');
});

演示: http://jsfiddle.net/PFBVX/


在旧版本的 jQuery 中,执行此操作...

$('div.reflection').each(function() {
    $(this).after($(this).clone().toggleClass('reflection reflected'));
});

演示: http://jsfiddle.net/PFBVX/1/

Pass a function to .after(), and have it return the updated clone.

$('div.reflection').after(function() {
    return $(this).clone().toggleClass('reflection reflected');
});

DEMO: http://jsfiddle.net/PFBVX/


In older versions of jQuery, do this...

$('div.reflection').each(function() {
    $(this).after($(this).clone().toggleClass('reflection reflected'));
});

DEMO: http://jsfiddle.net/PFBVX/1/

陈年往事 2025-01-10 23:36:57

我相信您需要 .each() 来单独处理它们。代码当前所做的就是获取所有这些并在所有这些之后克隆它们。相反,您希望单独处理每个类,并在根据需要更改类之后将克隆插入到原始类之后。

$(document).ready(function(){

    $('div.reflection').each(function () {
        var org = $(this),
            clone = org.clone();

        clone.removeClass('reflection').addClass('reflected')
            .insertAfter(org);
    });

});

I believe you'll need to .each() of them to handle them individually. What the code is currently doing is grabbing all of them and cloning them after all of them. Instead, you want to handle each of them individually and insert the clone after the original, after changing the classes around as desired.

$(document).ready(function(){

    $('div.reflection').each(function () {
        var org = $(this),
            clone = org.clone();

        clone.removeClass('reflection').addClass('reflected')
            .insertAfter(org);
    });

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