js根据坐标生成文本框
时间: 2023-10-04 09:08:49 浏览: 68
可以使用 JavaScript 中的 DOM 操作来生成文本框,并设置其坐标位置。具体实现步骤如下:
1. 创建一个文本框元素
```javascript
var textBox = document.createElement("input");
textBox.type = "text"; // 设置输入框类型为文本框
```
2. 设置文本框的位置和尺寸
```javascript
textBox.style.position = "absolute"; // 设置绝对定位
textBox.style.left = x + "px"; // 设置 x 坐标
textBox.style.top = y + "px"; // 设置 y 坐标
textBox.style.width = width + "px"; // 设置宽度
textBox.style.height = height + "px"; // 设置高度
```
其中,`x` 和 `y` 分别为文本框的左上角坐标,`width` 和 `height` 分别为文本框的宽度和高度。
3. 将文本框添加到页面中
```javascript
document.body.appendChild(textBox);
```
完整的代码示例:
```javascript
var x = 100; // x 坐标
var y = 100; // y 坐标
var width = 200; // 宽度
var height = 30; // 高度
var textBox = document.createElement("input");
textBox.type = "text";
textBox.style.position = "absolute";
textBox.style.left = x + "px";
textBox.style.top = y + "px";
textBox.style.width = width + "px";
textBox.style.height = height + "px";
document.body.appendChild(textBox);
```
这样就可以根据坐标生成文本框了。