我是靠谱客的博主 外向鱼,这篇文章主要介绍元素未显示设置width/height时IE中使用currentStyle获取为auto,现在分享给大家,希望可以做个参考。

我们知道获取元素的实际宽高在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 style="width:100px;height:50px;">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

最后

以上就是外向鱼最近收集整理的关于元素未显示设置width/height时IE中使用currentStyle获取为auto的全部内容,更多相关元素未显示设置width/height时IE中使用currentStyle获取为auto内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(96)

评论列表共有 0 条评论

立即
投稿
返回
顶部