javascript - 将css宽度作为数字而不是百分比
问题描述
在我的代码的这个简化版本中,我试图将 css 宽度提取为没有附加百分比的数字。研究一个解决方案,我发现了这个:获取一个没有“px”的样式值的数字;解决问题的后缀。但是,在尝试重现相同的结果时,我得到了
barSize: NaN
代替
barSize: 90;
更新:这个问题不是在问如何获取属性
document.querySelector('.defaultBar').style.width
这使:
barSize: 90%
但是如何在没有后缀的情况下获得属性
parseInt(document.querySelector('.defaultBar').style.width, 10);
这应该给:barSize:90
没有%后缀,而是给出NaN。
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style>
.defaultBar {
background-color: green;
height: 4px;
width: 90%;
}
</style>
</head>
<body>
<div class="defaultBar"></div>
<script>
const barSize = parseInt(document.querySelector('.defaultBar').style.width, 10);
console.log("barSize: ", barSize);
</script>
</body>
</html>
解决方案
推荐阅读
- ruby-on-rails - controller#create 不会保存,@product.save 不会工作
- ios - 有许多嵌套的 UITableViews 是否可以,或者有更好的方法来实现这一点?
- wordpress - 特色图片 Wordpress - 自动放大和隐藏 placehold.it
- python - 为什么 numpy 的 fft 的每一秒系数都是它应该是的倒数?
- jquery - jquery初始化控制台日志
- android-fragments - 与活动运行时的片段通信(在获取用户输入后)
- amazon-web-services - 如何从 aws cloudwatch 获取指标数据到 csv
- java - 如何使用 gradle + spring boot 1.5 构建子模块的可执行 jar
- laravel - Laravel 视图错误
- javascript - Jquery first() 不工作