javascript - 我如何不显示价格超过的元素???从容器
问题描述
我想要隐藏价格超过 150 的每个元素的功能。我制作了从容器中制作商店物品的脚本。现在我想制作价格类别,所以 onclick 它只显示 150 以下的项目。
var ItemCollection =
[
{
Name: 'IVY PA',
Price: 160,
Picture: 'some pic'
},
{
Name: 'IVY P',
Price: 100,
Picture: 'some pic'
},
];
ItemCollection.forEach(generateItem);
$('#Collection').append(content);
var content = "";
function generateItem(item, index, arrays) {
content = content + '\n' +
' <li class="Shoe"><a href="#' + item.Name + '\'">\n' +
' <div>\n' +
' <img class="ItemPicture"\n' +
' src=" ' + item.Picture + '">\n' +
'\n' +
' </div>\n' +
' <div class="ItemInfo">\n' +
' <p>' + item.Name + '</p>\n' +
' <p>' + item.Price + '$</p>\n' +
' </div>\n' +
' </a>\n' +
' </li>\n';
}
function under150() {
var price = "";
var Shoe = document.getElementsByClassName("Shoe");
price = ItemCollection.item.Price;
console.log(ItemCollection.item.Price);
if (price > 150){
Shoe.style.display = "none";
}
else {}
}
这是我目前的尝试,但我知道这是完全错误的,我需要一点帮助,所以如果你有任何想法,我将非常感激。
解决方案
迭代每个项目,然后检查价格,如果价格超过 150,则隐藏该特定项目。
function under150() {
var Shoe = document.getElementsByClassName("Shoe");
for(var i=0;i<Shoe.length;i++)
{
price = ItemCollection[i].Price;
if (price > 150){
Shoe[i].style.display = "none";
}
}
}
推荐阅读
- javascript - JavaScript - Canvas:为什么最后一个箭头的颜色会改变所有箭头和半圆?
- javascript - “then”编译成什么,以便在旧浏览器中工作?
- javascript - 在另一个函数中使用一个函数不起作用:未定义变量
- javascript - ReactJS 从地图中唯一地选择一个元素
- python - 如何从链接到另一个模型的外键访问模型字段?
- c# - 关于将数据从控制器传递到asp.net mvc 5中的布局子视图的进一步说明
- javascript - 如何从 Dialogflow 中的先前上下文中获取参数
- spring - 如何更改 gradle 依赖的传递版本?
- python - 是否可以在 NLTK punkt word_tokenize 中仅使用 english.pickle 来减少部署包的大小?
- python - 带有可选参数的继承链