Javascript 如何基于最高z索引获取元素属性
我不知道如何实现,我有一些元素具有Javascript 如何基于最高z索引获取元素属性,javascript,Javascript,我不知道如何实现,我有一些元素具有common\u class类名,我想获得最高z索引元素的ID,可能吗 function findHighestZIndex(elem) { var elems = document.getElementsByClassName(elem); var highest = 0; for (var i = 0; i < elems.length; i++) { var id = document.getElementsByClassNa
common\u class
类名,我想获得最高z索引元素的ID
,可能吗
function findHighestZIndex(elem)
{
var elems = document.getElementsByClassName(elem);
var highest = 0;
for (var i = 0; i < elems.length; i++)
{
var id = document.getElementsByClassName(elem);
id.getAttribute("id");
console.log(id);
var zindex=document.defaultView.getComputedStyle(elems[i],null).getPropertyValue("z-index");
var ElementDisplay = document.defaultView.getComputedStyle(elems[i],null).getPropertyValue("display");
if ((zindex > highest) && (zindex != 'auto') && (ElementDisplay == 'block'))
{
highest = zindex;
}
函数findHighestZIndex(elem)
{
var elems=document.getElementsByClassName(elem);
var最高=0;
对于(变量i=0;ihighest)&&&(zindex!=“auto”)&&&(ElementDisplay==“block”))
{
最高=锌指数;
}
}您可以使用以下功能:
function isNumeric(val) {
return !isNaN(parseFloat(val)) && isFinite(val);
}
function findHighestZIndex(className) {
let queryObject = document.getElementsByClassName(className);
let childNodes = Object.keys(queryObject).map(key => queryObject[key]);
let highest = 0;
var highestElem;
childNodes.forEach((node) => {
// Get the calculated CSS z-index value.
let cssStyles = document.defaultView.getComputedStyle(node);
let cssZIndex = cssStyles.getPropertyValue('z-index');
// Get any inline z-index value.
let inlineZIndex = node.style.zIndex;
// Coerce the values as integers for comparison.
cssZIndex = isNumeric(cssZIndex) ? parseInt(cssZIndex, 10) : 0;
inlineZIndex = isNumeric(inlineZIndex) ? parseInt(inlineZIndex, 10) : 0;
// Take the highest z-index for this element, whether inline or from CSS.
let currentZIndex = cssZIndex > inlineZIndex ? cssZIndex : inlineZIndex;
if ((currentZIndex > highest)) {
highest = currentZIndex;
highestElem = node;
}
});
var obj = {highestZIndex: highest, elem: highestElem};
return obj;
}
1.
2.
3.
函数为数值(val){
return!isNaN(parseFloat(val))和&isFinite(val);
}
函数findHighestZIndex(类名){
让queryObject=document.getElementsByCassName(类名称);
让childNodes=Object.keys(queryObject.map)(key=>queryObject[key]);
设最高值=0;
var highestElem;
childNodes.forEach((节点)=>{
//获取计算出的CSS z索引值。
让cssStyles=document.defaultView.getComputedStyle(节点);
设cssZIndex=cssStyles.getPropertyValue('z-index');
//获取任何内联z索引值。
让inlineZIndex=node.style.zIndex;
//强制将值作为整数进行比较。
cssZIndex=isNumeric(cssZIndex)?parseInt(cssZIndex,10):0;
inlineZIndex=isNumeric(inlineZIndex)?parseInt(inlineZIndex,10):0;
//获取此元素的最高z索引,无论是内联的还是来自CSS的。
让currentZIndex=cssZIndex>inlineZIndex?cssZIndex:inlineZIndex;
如果((当前Zindex>最高)){
最高=当前Zindex;
highestElem=节点;
}
});
var obj={highestZIndex:highest,elem:highestElem};
返回obj;
}
console.log(findHighestZIndex('test').elem.id);
您可以使用以下功能:
function isNumeric(val) {
return !isNaN(parseFloat(val)) && isFinite(val);
}
function findHighestZIndex(className) {
let queryObject = document.getElementsByClassName(className);
let childNodes = Object.keys(queryObject).map(key => queryObject[key]);
let highest = 0;
var highestElem;
childNodes.forEach((node) => {
// Get the calculated CSS z-index value.
let cssStyles = document.defaultView.getComputedStyle(node);
let cssZIndex = cssStyles.getPropertyValue('z-index');
// Get any inline z-index value.
let inlineZIndex = node.style.zIndex;
// Coerce the values as integers for comparison.
cssZIndex = isNumeric(cssZIndex) ? parseInt(cssZIndex, 10) : 0;
inlineZIndex = isNumeric(inlineZIndex) ? parseInt(inlineZIndex, 10) : 0;
// Take the highest z-index for this element, whether inline or from CSS.
let currentZIndex = cssZIndex > inlineZIndex ? cssZIndex : inlineZIndex;
if ((currentZIndex > highest)) {
highest = currentZIndex;
highestElem = node;
}
});
var obj = {highestZIndex: highest, elem: highestElem};
return obj;
}
1.
2.
3.
函数为数值(val){
return!isNaN(parseFloat(val))和&isFinite(val);
}
函数findHighestZIndex(类名){
让queryObject=document.getElementsByCassName(类名称);
让childNodes=Object.keys(queryObject.map)(key=>queryObject[key]);
设最高值=0;
var highestElem;
childNodes.forEach((节点)=>{
//获取计算出的CSS z索引值。
让cssStyles=document.defaultView.getComputedStyle(节点);
设cssZIndex=cssStyles.getPropertyValue('z-index');
//获取任何内联z索引值。
让inlineZIndex=node.style.zIndex;
//强制将值作为整数进行比较。
cssZIndex=isNumeric(cssZIndex)?parseInt(cssZIndex,10):0;
inlineZIndex=isNumeric(inlineZIndex)?parseInt(inlineZIndex,10):0;
//获取此元素的最高z索引,无论是内联的还是来自CSS的。
让currentZIndex=cssZIndex>inlineZIndex?cssZIndex:inlineZIndex;
如果((当前Zindex>最高)){
最高=当前Zindex;
highestElem=节点;
}
});
var obj={highestZIndex:highest,elem:highestElem};
返回obj;
}
console.log(findHighestZIndex('test').elem.id);
如果您指的是此处未获取id值:
var elems = document.getElementsByClassName(elem);
var highest = 0;
for (var i = 0; i < elems.length; i++)
{
//This is all wrong here, commenting it out
//var id = document.getElementsByClassName(elem); //You already have this collection
//id.getAttribute("id"); //The above returns a collection so this would fail, you'd need to use an index of the collection
//console.log(id);
//You already have the elements you want, just use the i index to grab the element
//and it's id
console.log(elems[i].id);
var elems=document.getElementsByClassName(elem);
var最高=0;
对于(变量i=0;i
如果您指的是此处未获取id值:
var elems = document.getElementsByClassName(elem);
var highest = 0;
for (var i = 0; i < elems.length; i++)
{
//This is all wrong here, commenting it out
//var id = document.getElementsByClassName(elem); //You already have this collection
//id.getAttribute("id"); //The above returns a collection so this would fail, you'd need to use an index of the collection
//console.log(id);
//You already have the elements you want, just use the i index to grab the element
//and it's id
console.log(elems[i].id);
var elems=document.getElementsByClassName(elem);
var最高=0;
对于(变量i=0;i
您可以修改函数以返回具有最高z索引的元素,然后只需使用.id
获取其id
另一个问题:您正在将zindex
和highest
作为字符串而不是数字进行比较。在比较之前,我在zindex
前面加了一元+
运算符,就像您的示例中那样,如果您将9
的z索引和1000
的z索引进行比较,它会相信9
更大
例如:
函数findHighestZIndex(elem){
var最高=0;
var highestElement=null;
var elems=document.getElementsByClassName(elem);
对于(变量i=0;ihighest)&&(ElementDisplay='block')){
最高=锌指数;
高级元素=元素[i];
}
}
返回高度元素;
}
var elem=findHighestZIndex(“zindex”);
console.log(elem.id+“具有最高的z索引”);
div.zindex{
z指数:500;
位置:绝对位置;
显示:块;
}
您可以修改函数以返回