为什么宽度样式值不显示在输入中?
Why width style value not show in input?
我正在尝试从 "demo" div 中获取宽度并将其放入 "input number" 值中。我想不通我哪里弄错了...
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>
这个问题是因为 width
的值是 35px
所以你应该在将它分配给 document.getElementById("demo").value
之前删除 px
以便你的 input
尽快成为数字类型是 number
如下代码
<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>
<script>
function myFunction() {
var element = document.getElementById("poster"),
style = window.getComputedStyle(element),
width = style.getPropertyValue('width').match(/\d+/);
document.getElementById("demo").value = width;
}
</script>
如果你想获得没有单位的宽度数字,例如。 35px
或 35%
将得到 35
您可以使用以下代码:
<div id="poster" style="width:35px;height:45px;background-color:blue;"></div>
<input id="demo" type="number" placeholder="0" min="" max="" step="0.1">
<select id="unit" >
<option >none</option>
<option value="%">%</option>
<option value="px">px</option>
</select>
<button onclick="myFunction()" type="button">Click Me!</button>
<script>
function myFunction() {
var element = document.getElementById("poster"),
width = element.style["width"].match(/\d+/),
unit = element.style["width"].match(/[^\d]+/);
document.getElementById("unit").value = unit
document.getElementById("demo").value = width;
}
</script>
我正在尝试从 "demo" div 中获取宽度并将其放入 "input number" 值中。我想不通我哪里弄错了...
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>
这个问题是因为 width
的值是 35px
所以你应该在将它分配给 document.getElementById("demo").value
之前删除 px
以便你的 input
尽快成为数字类型是 number
如下代码
<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>
<script>
function myFunction() {
var element = document.getElementById("poster"),
style = window.getComputedStyle(element),
width = style.getPropertyValue('width').match(/\d+/);
document.getElementById("demo").value = width;
}
</script>
如果你想获得没有单位的宽度数字,例如。 35px
或 35%
将得到 35
您可以使用以下代码:
<div id="poster" style="width:35px;height:45px;background-color:blue;"></div>
<input id="demo" type="number" placeholder="0" min="" max="" step="0.1">
<select id="unit" >
<option >none</option>
<option value="%">%</option>
<option value="px">px</option>
</select>
<button onclick="myFunction()" type="button">Click Me!</button>
<script>
function myFunction() {
var element = document.getElementById("poster"),
width = element.style["width"].match(/\d+/),
unit = element.style["width"].match(/[^\d]+/);
document.getElementById("unit").value = unit
document.getElementById("demo").value = width;
}
</script>