使用输入控件动态操作元素属性?
Dynamically manipulate element attributes using input controls?
我想使用输入控件来动态操作元素。纯 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>
结果会是这样的:
您所要做的就是添加一些 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>
我想使用输入控件来动态操作元素。纯 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>
结果会是这样的:
您所要做的就是添加一些 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>