【发布时间】:2020-11-19 17:40:16
【问题描述】:
如何将以下条件附加到 id = #sellerMetal <ul> 容器,它是一个动态循环列表。
如果 dataJS
productList > 奖牌 > 金牌 = true,
追加<li class="icon"><img src="/img/goldmetal.png"></li>
如果 dataJS
productList.medals.silvermedal = true,
追加<li class="icon"><img src="/img/silvermedal.png"></li>
如果 dataJS
productList > 奖牌 > newshop= true,
追加<li class="icon"><img src="/img/newshop.png"></li>
...
...
...
...
...
这是产品列表。
下面是 Product List Append 的渲染列表。
预期结果:
<div id="List">
<div class="feature_ico">
<ul id="#sellerMetal" class="icons">
(Apend to here)
</ul>
</div>
<div class="feature_ico">
<ul id="#sellerMetal" class="icons">
(Apend to here)
</ul>
</div>
<div class="feature_ico">
<ul id="#sellerMetal" class="icons">
(Apend to here)
</ul>
</div>
<div class="feature_ico">
<ul id="#sellerMetal" class="icons">
(Apend to here)
</ul>
</div>
</div>
下面是示例 sn-p。
以下是我尝试过的代码:comment //Condition is here I have tried 是我提出的条件,它不像预期的那样工作。
//Below is the Data JS
var data = { productList: [
{
id: "e0c18c12-0b51-41ad-9f2c-aefc20cafcdb",
link: "1",
imgurl: "img001",
text: 'Product 001',
seo: '',
medals: [{
goldmedal: true,
sellermedal: false,
newshop: true
}]
},
{
id: "f3bee385-76d7-4a87-8bba-a51d33238858",
link: "2",
imgurl: "img002",
text: 'Product 002',
seo: '',
medals: [{
goldmedal: true,
sellermedal: true,
newshop: true
}]
},
{
id: "fc3dd6a9-5d8c-4262-aa65-a680e0f0cafe",
link: "3",
imgurl: "img003",
text: 'Product 003',
seo: '',
medals: [{
goldmedal: false,
sellermedal: false,
newshop: true
}]
},
{
id: "8615711e-8544-4484-933f-b14a93941b86",
link: "4",
imgurl: "img004",
text: 'Product 004',
seo: '',
medals: [{
goldmedal: false,
sellermedal: false,
newshop: true
}]
}]
};
$(function () {
const getProductList = function (productItem) {
const productListRender = $('<div>', { class: 'feature_ico'}).append($('<ul>', {
id: plSettings.sellerMetal,
class: 'icons'
}));
//Condition is here i have tried
if (productItem.medals) {
$.each(productItem.medals, function (index, data) {
if (this.goldmedal === true) {
const medalRender = $(plSettings.sellerMetal);
const metalItem = $('<li>', {
class: 'icon gold'
}).append($('img'), {
src: ''
});
medalRender.append(metalItem);
} else if (this.silvermedal === true) {
const medalRender = $(plSettings.sellerMetal);
const metalItem = $('<li>', {
class: 'icon gold'
}).append($('img'), {
src: ''
});
medalRender.append(metalItem);
}
});
}
return productListRender;
};
const $product = $('#List')
$.each(data.productList, function (index, data) {
$product.append(getProductList(data));
});
});
//Below is the Setting JS
var plSettings = $.extend({
sellerMetal: '#' + 'sellerMetal',
goldMetalSrc: '/img/tps/gold.png'
});
.feature_ico {
border: 1px solid #e0e0e0
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="List"></div>
【问题讨论】:
标签: javascript html jquery web dynamic