我想从HTML元素中获取显示属性。当我编写内联CSS时,它可以工作,但是如果我使用一个类,它就不能工作。
这是可行的:
<p id="p1" style="display:none;">This is some text.</p>
<script>alert(document.getElementById("p1").style.display);</script>http://jsfiddle.net/bwzAN/2/
这不起作用:
<style>.deneme{ display: none; }</style>
<p id="p1" class="deneme">This is some text.</p>
<script>alert(document.getElementById("p1").style.display);</script>http://jsfiddle.net/bwzAN/7/
为什么?有没有可能让第二种情况像第一种情况一样呢?我怎么才能修复它?
发布于 2012-09-12 04:12:53
用getComputedStyle() - 尝试一下
$(document).ready(function(){
var elem = document.getElementById("p1");
var st = window.getComputedStyle(elem, null).getPropertyValue("display");
alert( st );
});https://stackoverflow.com/questions/12377141
复制相似问题