gpt4 book ai didi

javascript - 如何附加所有相同的类jquery?

转载 作者:行者123 更新时间:2023-11-28 19:08:58 25 4
gpt4 key购买 nike

在我的网站中,我想将下面的 div 附加到一个类中,我有很多相同的类。该网站正在销售 T 恤,每件商品都有 .color 等级。每次,当我刷新页面时,它只附加到一个项目,当我转到第二个项目时,div 不会附加。 #myshop 正在从其他站点加载,因此这就是我需要使用追加来添加消息的原因。我使用setTimeout的原因是因为#myshop是在页面加载之后加载的,所以我必须延迟它。你可以在我的网址www.sayhitoworld.com上查看,点击任意一项,你会看到颜色下面的消息,返回,然后点击另一个项目,消息不会再追加。感谢您的时间。

jQuery('#myshop').one('click', function(){
setTimeout(append_color, 1000);
});

function append_color(){
jQuery('.color').append("<div id='append_color' >Note: If the T-shirt had the same color as the character's hair or body, you won't see the shape, still cool though.</div>");

}

最佳答案

这些项目是否在同一页面上?如果是这样,这个 div 只会附加到第一个项目,因为 div 有一个 ID。 ID 需要是唯一的,因此每一页只能存在一个名为append_color 的div。为什么不附加一个名为append_color 的类,看看它是否适合您。

function append_color(){
$('.color').each(function(){
$(this).append("<div class='append_color' >Note: If the T-shirt had the same color as the character's hair or body, you won't see the shape, still cool though.</div>");
});
}

关于javascript - 如何附加所有相同的类jquery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31122448/

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