gpt4 book ai didi

javascript - 访问 div 的子元素并附加 span

转载 作者:太空宇宙 更新时间:2023-11-03 20:42:31 26 4
gpt4 key购买 nike

我正在像这样访问我的 child ,我正在尝试为每个 div 容器设置跨度标题。

$(".div-container").children().each(function(n, i){    
console.log("Is it coming" + this.id);
$(this.id).before($('<span />').css('margin-left' , '60px').attr({'class':'progress-title' }).html('Title'));
$(this.id).before($('<span />').attr({'class':'progress-title-after' }).html('25% Usage'));
});

我得到了相应的 id,但是 span 元素没有被添加到我的 div 容器中。

最佳答案

this 引用该元素,因此您无需通过 ID 重新选择它。只需将 this 包装在 jQuery 对象中即可。

$(this).before($('<span />').css('margin-left' , '60px').attr({'class':'progress-title' }).html('Title'));

关于javascript - 访问 div 的子元素并附加 span,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24609570/

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