gpt4 book ai didi

javascript - 如何克隆和进行多次替换

转载 作者:行者123 更新时间:2023-12-03 11:59:36 25 4
gpt4 key购买 nike

这是我的JS,我正在克隆div #product-1,新克隆当前已更改标签,我想知道如何进行额外的替换。

var clone = $('#product-1').clone(false);
clone.find(".ingredientLabel").html(clone.find(".ingredientLabel").html().replace(/[0-9]+/g, counter));
$(clone).appendTo(".activeingredients");

如何将 ID 从 product-1 更改为 'product-'+counter

最佳答案

使用attr()在使用 find()

向下转换到克隆的 dom 元素之前更改 ID 属性
clone.attr('id', 'product-'+counter).find(".ingredientLabel").html(clone.find(".ingredientLabel").html().replace(/[0-9]+/g, counter));

一旦您使用 find(),.ingredientLabel 将成为您将方法链接到的选定对象。

将方法链接到clone,以在选择.ingredientLabel之前选择#product-1

关于javascript - 如何克隆和进行多次替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25474639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com