2 回答
TA贡献1799条经验 获得超9个赞
您忘记了图像数组的索引
window.onload = function() {
var i, img;
var img = document.getElementsByClassName("my_pictures_class");
for (i = 0; i < img.length; i++) {
var width = img[i].clientWidth;
var height = img[i].clientHeight;
if (height > width) {
img[i].style.display = "none";
}
}
};
考虑每个
const imgList = document.getElementsByClassName("my_pictures_class");
Array.from(imgList).forEach(img => {
const height = img.clientHeight
const width = img.clientWidth
if (height > width) {
img.style.display = "none";
}
})
TA贡献1772条经验 获得超5个赞
您错过了提及索引。尽管您可以通过使用querySelectorAll()and来避免使用索引forEach(),如下所示:
window.onload = function() {
var imgList = document.querySelectorAll(".my_pictures_class");
imgList.forEach(function(img){
var width = img.clientWidth;
var height = img.clientHeight;
if (height>width){
img.style.display = "none";
}
else{
//Nothing
}
});
};
<img src="https://homepages.cae.wisc.edu/~ece533/images/cat.png" class="my_pictures_class">
<img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="my_pictures_class">
<img src="https://homepages.cae.wisc.edu/~ece533/images/arctichare.png" class="my_pictures_class">
<img src="https://homepages.cae.wisc.edu/~ece533/images/serrano.png" class="my_pictures_class">
<img src="https://homepages.cae.wisc.edu/~ece533/images/boat.png" class="my_pictures_class">
- 2 回答
- 0 关注
- 101 浏览
添加回答
举报