简体   繁体   English

如何使用输入字段动态更改宽度和高度?

[英]How do I change dynamically the width and height with an input field?

So I've created a form, which posses an Input field, that Input field should always have a default value (10px width 10px height).所以我创建了一个表单,它拥有一个输入字段,该输入字段应该总是有一个默认值(10px 宽 10px 高)。 One input field is for the height the secound for the width for the div.一个输入字段是高度,第二个是 div 的宽度。 I want that if someone changes on of these two values that the div.box react to it.我希望如果有人更改 div.box 对它做出反应的这两个值。 Like if the input is filled with a number 20px width, it should take 20px as width.就像输入填充了一个 20px 宽度的数字一样,它应该采用 20px 作为宽度。 This all should happen without a page reload..这一切都应该在没有页面重新加载的情况下发生..

How do I archive that?我该如何存档?

 #box { width: 100px; height: 100px; background: red; }
 <form> <label for="width">Width</label><br> <input type="number" id="width" name="width"><br> <label for="height">Height</label><br> <input type="number" id="height" name="height"><br> <button type="submit">Submit</button> </form> <div id="box"></div>

Codepen: https://codepen.io/toonytest/pen/poeyywZ代码笔: https://codepen.io/toonytest/pen/poeyywZ

 ['width', 'height'].forEach(dimension => { window[dimension].addEventListener('change', ({target: { value } }) => { box.style[dimension] = value + 'px' }) })
 #box { width: 10px; height: 10px; background: red; }
 <form> <label for="width">Width</label><br> <input type="number" id="width" name="width" value=10><br> <label for="height">Height</label><br> <input type="number" id="height" name="height" value=10><br> <button type="submit">Submit</button> </form> <div id="box"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM