使用添加到数组中的元素(document.getElementById('ID')) [英] Using elements that are added to an array with (document.getElementById('ID'))

查看:514
本文介绍了使用添加到数组中的元素(document.getElementById('ID'))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这段代码不起作用?

Why does this code not work?

var all_obj_element= new Array();
all_obj_element[0]= document.getElementById('Img3');            
alert(all_obj_element[0].style.width);

警告显示一个空框!

推荐答案

因为你没有设置宽度。以下是获取元素的计算样式值的方法:

Because you haven't set the width. Here is how you get the computed style value of an element:

var computedStyle = function (el,style) {
    var cs;
    if (typeof el.currentStyle != 'undefined'){
        cs = el.currentStyle;
    }
    else {
        cs = document.defaultView.getComputedStyle(el,null);
    }
    return  cs[style];
}

现在让我们得到价值:

var element = document.getElementById('Img3');

alert(computedStyle(element,'width'));

这篇关于使用添加到数组中的元素(document.getElementById('ID'))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆