javascript - 为什么宽度样式值不显示在输入中?
问题描述
我正在尝试从“演示”div 中获取宽度并将其放入“输入数字”值中。我不知道我在哪里犯了错误......
function myFunction() {
var element = document.getElementById("poster"),
style = window.getComputedStyle(element),
width = style.getPropertyValue('width');
document.getElementById("demo").value = width;
}
<div id="poster" style="width:35px;height:45px;background-color:blue;"></div>
<input id="demo" type="number" placeholder="0" min="" max="" step="0.1">
<button onclick="myFunction()" type="button">Click Me!</button>
解决方案
您需要转换width
为,number
因为您input number
不使用文本。实际上,你width
是一个字符串(35px)。
function myFunction() {
var element = document.getElementById("poster"),
style = window.getComputedStyle(element),
width = style.getPropertyValue('width');
document.getElementById("demo").value = parseFloat(width);
}
<div id="poster" style="width:35px;height:45px;background-color:blue;"></div>
<input id="demo" type="number" placeholder="0" min="" max="" step="0.1">
<button onclick="myFunction()" type="button">Click Me!</button>
推荐阅读
- macos - 我如何让自己摆脱 Apple Developer ID 证书的混乱
- xamarin - Xamarin Forms Custom Renderer iOS UICollectionView 双向滚动/水平 - 垂直滚动
- javascript - 使用车把保持滚动位置
- javascript - 要从映射中获取不同字符串的数组,从映射值的属性中提取字符串
- binaryfiles - 计算包含某些字节的二进制文件的哈希
- sql - 你如何引用来自不同表单视图(ASP.NET)的数据
- ios - Near IF 语法错误(SQLite3+Objective-C)(如果不存在则更新)
- json - Apache Camel:XML 到 JSON 的转换
- jquery - 如何在没有 Jquery 值的情况下将选择行添加到 div 中?
- python - from anshu.myapp.views import TaskViewSet ModuleNotFoundError: No module named 'anshu.myapp'