gpt4 book ai didi

javascript - 使用值创建新的 div 属性

转载 作者:行者123 更新时间:2023-11-28 04:45:37 24 4
gpt4 key购买 nike

我有以下创建新 Div 的 jQuery 代码,并将其附加到具有某些属性的父级。

 $.each(data.ConsultantDetails.ScopeOfSevrices, function (index) {
$("#parentOptionDiv").append($('<div/>', {
"id": data.ConsultantDetails.ScopeOfSevrices[index].Value,
"class": 'item',
"data-value":''
}))
});

它呈现 HTML

<div id="9" class="item" data-value=""></div>

如何将值放在 <div> My Value </div> 之间

预期

<div id="9" class="item" data-value="">MY Value</div>

最佳答案

使用 text 选项:

$.each(data.ConsultantDetails.ScopeOfSevrices, function (index) {
$("#parentOptionDiv").append($('<div/>', {
"id": data.ConsultantDetails.ScopeOfSevrices[index].Value,
"class": 'item',
"data-value":'',
"text": "My Value"
}))
});

the documentation 涵盖了这一点对于这种形式的 $() 调用:

As of jQuery 1.4, any event type can be passed in, and the following jQuery methods can be called: val, css, html, text, data, width, height, or offset.

关于javascript - 使用值创建新的 div 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47429755/

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