使用输入控件动态操纵元素属性?

时间:2019-03-31 01:50:28

标签: javascript jquery forms dom element

我想使用输入控件动态地操作元素。纯JavaScript或jQuery都可以。

 src="https://www.w3schools.com/css/rock600x400.jpg" >
</br><br>
<form class="button" action="">
Width
<input class="button" type="number" value="300">
Height  
<input class="button" type="number" value="400">
 </form>

结果将是这样的:

Example result

1 个答案:

答案 0 :(得分:1)

您要做的就是添加一些id属性,在JavaScript中引用您的元素,并更新图像样式oninput(更改宽度或高度的值)

let img = document.getElementById('img');
let width = document.getElementById('width');
let height = document.getElementById('height');

changeSize = () => {
  img.style.width = `${width.value}px`;
  img.style.height = `${height.value}px`;
}
<img id="img" src="https://www.w3schools.com/css/rock600x400.jpg" />
</br><br>
<form class="button" action="">
  Width
  <input id="width" class="button" type="number" value="300" oninput="changeSize()"> Height
  <input id="height" class="button" type="number" value="400" oninput="changeSize()">
</form>