HTML5动态创建文本框数组,并设置文本框的文本
HTML5 Create an array of textboxes dynamically and set text of the text box
我有一个文本框中显示的单词列表
<div id = "rightbox" ondrop="drop(event)" ondragover="allowDrop(event)">
<div><input type="text" id="appleword" value="apple" class="textbox" readonly="ture" draggable="true" ondragstart="drag(event)"></div>
<div><input type="text" id="orangeword" value="orange" class="textbox" readonly="ture" draggable="true" ondragstart="drag(event)"></div>
<div><input type="text" id="peachword" value="peach" class="textbox" readonly="ture" draggable="true" ondragstart="drag(event)"></div>
</div>
需要帮助动态创建具有不同值的类似文本框(在数组中)
var words = ['apple', 'orange', 'peach'], // add more to array if needed
newInputs = document.createDocumentFragment(); // fragment to collect new inputs
// Loop through array of words and generate inputs
words.forEach(function (word) {
var wrapper = document.createElement('div'),
fieldSet = document.createElement('fieldset'),
input = document.createElement('input'); // Inputs default to type=text
// Decorate elements with needed attributes here (abbreviated)
wrapper.id = word + 'word';
input.id = word + 'input';
input.setAttribute('value', word);
input.setAttribute('class', 'textbox');
input.readOnly = true;
// Nest all these elements and add them to the fragment
newInputs.appendChild(wrapper).appendChild(fieldSet).appendChild(input);
});
// Insert the fragment into the DOM
document.getElementById('rightbox').appendChild(newInputs)
根据需要调整代码以添加缺失的属性和事件处理程序。如果您必须支持比IE9更早的东西,请将forEach
循环更改为for
循环。
相关文章:
- 使用jQuery重置文本,同时存在AJAX调用
- 使用VBScript或Javascript从文本框(文本区域)中删除最后一行空/空行
- EventSource 的响应具有非“文本/事件流”的 MIME 类型(“文本/纯文本”)
- 在鼠标悬停d3js上更改SVG文本的文本
- HTML文本区域文本到XML
- 我可以重置文本输入而不清除它吗
- Ng模型在AngularJS中重置文本区域后未更新
- 在 JavaScript 中重置文本区域高度
- 阻止按钮重置文本框值
- 阻止重置文本区域
- 输入后重置文本区域
- Extjs组合框在存储区未加载任何结果时保持重置文本值
- 使用javascript重置文本框密码
- 当提交按钮时,Javascript没有重置文本框中的内容
- 数据切换标题重置不重置文本
- 在asp.net中使用javascript通过客户端重置文本框
- 设置和重置文本点击按钮jquery
- 如何重置文本框并使文本框中的值等于“0”"(或空白)?(javascript)
- 在Safari中重置文本框值
- Firefox 不会在您重新加载页面时自动重置文本区域值