javascript - 添加<!doctype html>声明后,js获取不到css属性值
阿神
阿神 2017-04-11 11:20:49
[JavaScript讨论组]

为什么在添加声明后,js就获取不到get_x1.style.height呢?也无法get_x1.style.height修改height。
请看源码:







阿神
阿神

闭关修行中......

全部回复(4)
ringa_lee

你可以用console.log(get_x1.style);将style输出出来,就会发现很多属性
fontVariantLigatures:""
fontWeight:""
height:"200px"
imageRendering:""
isolation:""
justifyContent:""
left:""
length:1
letterSpacing:""
lightingColor:""

可以看到height后面是字符串,而length后面就是整型。所以一般height后面最好跟上"px"

PHPz

和<!doctype html>没关系吧,建议写成get_x1.style.height = "200px"

巴扎黑

改成 get_x1.style.height = '200px';

高洛峰

1.设置高的时候,代码改成get_x1.style.height = '200px';
2.获取高的时候,有clientHeight、offsetHeight、scrollHeight
修改后的代码:

<!doctype html>
<html>
<style>
*{margin:0;padding:0;border:0;}
#x1{width:100%;height:500px;background:grey;}
</style>

<body>
<p>
<p id="x1" class="x1"></p>
</p>
</body>

<script>
var h1 = document.body.scrollHeight;
var get_x1 = document.getElementById('x1');
get_x1.style.height = "200px";
alert(get_x1.clientHeight);
</script>

</html>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号