元素未显示设置width/height时IE中使用currentStyle获取为auto_Javascript教程-查字典教程网
元素未显示设置width/height时IE中使用currentStyle获取为auto
元素未显示设置width/height时IE中使用currentStyle获取为auto
发布时间:2016-12-30 来源:查字典编辑
摘要:我们知道获取元素的实际宽高在IE中可以使用currentStyle属性。但如果没有显示的去设置元素的宽高,那么使用该属性将获取不到,获取的值...

我们知道获取元素的实际宽高在IE中可以使用currentStyle属性。但如果没有显示的去设置元素的宽高,那么使用该属性将获取不到,获取的值为auto。如下

复制代码 代码如下:

<div>abcd</div>

<script>

var div = document.getElementsByTagName('div')[0];

alert(div.currentStyle.width);

alert(div.currentStyle.height);

</script>

IE6/7/8/9中输出的都是auto。如果显示的设置了宽高,那么输出的就是实际宽高。如下

1,通过内联style属性设置

复制代码 代码如下:

<div>abcd</div>

<script>

var div = document.getElementsByTagName('div')[0];

alert(div.currentStyle.width);

alert(div.currentStyle.height);

</script>

2,通过页面嵌入style标签设置

复制代码 代码如下:

<style>

div {

width: 100px;

height: 50px;

}

</style>

<div>abcd</div>

<script>

var div = document.getElementsByTagName('div')[0];

alert(div.currentStyle.width);

alert(div.currentStyle.height);

</script>

都将输出:100px,50px

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类