扫码关注官方订阅号
为什么在添加声明后,js就获取不到get_x1.style.height呢?也无法get_x1.style.height修改height。请看源码:
闭关修行中......
你可以用console.log(get_x1.style);将style输出出来,就会发现很多属性fontVariantLigatures:""fontWeight:""height:"200px"imageRendering:""isolation:""justifyContent:""left:""length:1letterSpacing:""lightingColor:""
可以看到height后面是字符串,而length后面就是整型。所以一般height后面最好跟上"px"
和<!doctype html>没关系吧,建议写成get_x1.style.height = "200px"
get_x1.style.height = "200px"
改成 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>
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
你可以用console.log(get_x1.style);将style输出出来,就会发现很多属性
fontVariantLigatures:""
fontWeight:""
height:"200px"
imageRendering:""
isolation:""
justifyContent:""
left:""
length:1
letterSpacing:""
lightingColor:""
可以看到height后面是字符串,而length后面就是整型。所以一般height后面最好跟上"px"
和<!doctype html>没关系吧,建议写成
get_x1.style.height = "200px"改成
get_x1.style.height = '200px';1.设置高的时候,代码改成
get_x1.style.height = '200px';2.获取高的时候,有clientHeight、offsetHeight、scrollHeight
修改后的代码: