javascript - 设置新的元素类属性值
问题描述
如何使用 javascript 将 10px 添加到 css 类属性?
例如:
var bigger = document.getElementsByClassName("size200").style.width;
bigger + 10;
解决方案
setTimeout(() => {
// get width
let width = window.getComputedStyle(document.querySelector('.some-class')).width;
width = +width.substr(0,width.length-2);
let newWidth = `${width+100}px`;
// set style
Array(document.getElementsByClassName('some-class').length)
.fill()
.map((_, index) => {
document.getElementsByClassName('some-class')[index].style.width = newWidth;
});
}, 1000)
.some-class {
width: 200px;
height: 200px;
background-color: red;
transition: all .5s;
}
<div class="some-class"></div>
<p>
wait 1s
</p>
<div class="some-class"></div>
推荐阅读
- c# - C# WPF - 动态改变单元格颜色
- java - 为什么 JNA 没有将 char** 映射到 String[]?
- apache-spark - SparkSQL over REST API 而不是 JDBC
- javascript - 使用 CropperJS 保存裁剪图像时无法读取未定义的 toBlob 属性
- java - Java 可选:更改值
- linux - 在 SLES 12-SP2 x86_64 上构建 openssl 1.0.2p 失败
- elm - 如何批量处理多个 http 调用?
- javascript - 如何以不同的名称安装相同的库
- scala - 如何将 Future[String] 转换为 String scala?
- laravel - 未定义的变量(Laravel 5.7)