无法获取显示属性 [英] Can't get display attribute

查看:46
本文介绍了无法获取显示属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 style.display 获取有关元素的 display 属性.但是它什么也没返回.看看JsFiddle.

I am trying to get display attribute about an element using style.display. But its not returning anything. Have a look at the JsFiddle one.

http://jsfiddle.net/4v5LS/1/

警报窗口中没有任何内容.

There is nothing in the alert window.

更新

现在我刚刚发现了一些不寻常的东西.每当我内联定义属性时,事情就起作用了.但是,当我以任何其他方法应用样式时,则无法正常工作.

Now I just noticed something unusual. Whenever I define the properties inline then things do work. But when I apply style in any other method, then its not working.

推荐答案

您可以使用 getComputedStyle()

document.getElementById('button').onclick = function() {
     element = document.getElementById('box');
     style = window.getComputedStyle(element),
     display = style.getPropertyValue('display');
    alert(display);
}

小提琴

这篇关于无法获取显示属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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