[英]How to let the user change the properties of a div with JavaScript?
you need to make javascript variables like 您需要使像
var box_height;
var box_width;
etc.. Then you need assign them the divs on your text boxes. 等等。然后您需要在文本框中为它们分配div。 Like:
喜欢:
box_height= document.getElementById('height_input_div').value;
box_width = document.getElementById('width_input_div').value;
etc.. 等等..
and then you should be able to change the properties of the box like this: 然后您应该能够像下面这样更改框的属性:
document.getElementById('myBox').style.height = box_height + "px";
You can tie all this to an onclick event by putting it all in a function like: 您可以将所有这些放到一个onclick事件中,方法是将它们全部放在类似的函数中:
function getBoxChanges()
{
document.getElementById('myBox').style.height = box_height + "px";
}
Got a working example up and running for you here . 在这里找到了一个可行的示例并为您运行。 I hope that is what you were looking for.
我希望那是您想要的。
HTML 的HTML
<table>
<tr>
<td>Left</td>
<td><input type="text" id="left-input"/>
</tr>
<tr>
<td>Top</td>
<td><input type="text" id="top-input"/>
</tr>
<tr>
<td>Width</td>
<td><input type="text" id="width-input"/>
</tr>
<tr>
<td>Height</td>
<td><input type="text" id="height-input"/>
</tr>
<tr>
<td>Color</td>
<td><input type="text" id="color-input"/>
</tr>
<tr>
<td>Background Color</td>
<td><input type="text" id="background-color-input"/>
</tr>
<tr>
<td>Text</td>
<td><input type="text" id="text-input"/>
</tr>
<tr>
<td></td>
<td><button type="button" id="save-button">Save</button></td>
</tr>
</table>
<div id="result"></div>
CSS 的CSS
td {
border: 1px solid black;
}
JavaScript 的JavaScript
const leftInput = document.getElementById('left-input');
const topInput = document.getElementById('top-input');
const widthInput = document.getElementById('width-input');
const heightInput = document.getElementById('height-input');
const colorInput = document.getElementById('color-input');
const textInput = document.getElementById('text-input');
const backgroundColorInput = document.getElementById('background-color-input');
const saveButton = document.getElementById('save-button');
const applyValues = () => {
const left = leftInput.value + "px";
const top = topInput.value + "px";
const width = widthInput.value + "px";
const height = heightInput.value + "px";
const color = colorInput.value;
const backgroundColor = backgroundColorInput.value;
const text = textInput.value;
/*
* You probably want to use `result.style.marginLeft` and `result.style.marginTop`.
*/
result.style.left = left;
result.style.top = top;
result.style.width = width;
result.style.height = height;
result.style.color = color;
result.style.backgroundColor = backgroundColor;
result.innerHTML = text;
console.log(result);
}
saveButton.addEventListener('click', applyValues);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.