gpt4 book ai didi

javascript - 根据它的 CSS 属性从数组中获取 Javascript 对象

转载 作者:太空宇宙 更新时间:2023-11-03 23:04:28 24 4
gpt4 key购买 nike

假设我在一个数组中有 3 个对象:

var redBoxx=document.getElementById("redBox");
var blueBoxx=document.getElementById("blueBox");
var orangeBoxx=document.getElementById("orangeBox");

var shapeArray = [redBoxx, blueBoxx, orangeBoxx];

我想根据对象的当前可见性状态(如可见性:“隐藏”或“可见”)从数组中抓取一个对象。我该怎么做?

最佳答案

尝试使用类似下面的东西

var redBox = document.getElementById('redBox');
var blueBox = document.getElementById('blueBox');
var orangeBox = document.getElementById('orangeBox');

var list = [redBox, blueBox, orangeBox];
Array.prototype.filterByProp = function(prop, value){
var currentStyle = window.getComputedStyle;
return this.filter(function(el){return value === currentStyle(el,null)[prop]});
}
//list.filter(function(el){return window.getComputedStyle(el,null).visibility === 'visible';})
console.log('hidden:' , list.filterByProp('visibility','hidden'));
console.log('Visible:' , list.filterByProp('visibility','visible'));
div{
box-sizing:border-box;
width:calc(100%/3);
height: 50vh;
border:1px solid #000;
float:left;
}
#redBox{
background:#f00;
}
#blueBox{
background:#00f;
visibility:hidden;
}
#orangeBox{
background:#f60;
}
}
<div id="redBox">
</div>
<div id="blueBox">
</div>
<div id="orangeBox">
</div>

关于javascript - 根据它的 CSS 属性从数组中获取 Javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232351/

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