简体   繁体   English

如何让用户使用JavaScript更改div的属性?

[英]How to let the user change the properties of a div with JavaScript?

The idea is like when the user changes value's in the table, they should apply to the div. 这个想法就像当用户更改表中的值时,他们应该应用于div。 This is for a school assignment and I can't find it online. 这是用于学校作业,我无法在线找到它。 The blue box is the div, the text on the left should change the properties: 蓝色框是div,左侧的文本应更改属性:

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.

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