如何在html中循环使用ID元素
How to loop through ID elements in html
我试图通过我的html中的所有ID字段循环,并使用JavaScript将它们打印到文本字段。这是我所拥有的:谢谢你的帮助:-)
**看起来你的大部分问题都是代码,请提供更多细节**
<html>
<head>
<title>Modifying Sentiment</title>
</head>
<body>
<body bgcolor="#E6E6FA">
<font color=black size=+3>Modifying Sentiment</font>
<table>
<tr><td>Text to Save:</td></tr>
<tr>
<td colspan="3">
Add positive adjective:
<img Adjective src="http://findicons.com/files/icons/2776/android_icons/96/ic_question_mark.png" alt="question" title="Adjective: is a word naming an attribute of a noun, such as sweet, red, or technical."
width=20 />
<br>
<textarea cols=40 rows=3 id="textBox1" ></textarea>
<p>
<textarea id="textBox2" style="width:512px;height:256px"></textarea>
</td>
</tr>
<tr>
<td>Filename to Save As:</td>
<td><input id="inputFileNameToSaveAs"></input></td>
<td><button onclick="saveTextAsFile()">Save Text to File</button></td>
</tr>
<tr>
<td>Select a File to Load:</td>
<td><input type="file" id="fileToLoad"></td>
<td><button onclick="loadFileAsText()">Load Selected File</button><td>
</tr>
</table>
<script type='text/javascript'>
function saveTextAsFile()
{
var el;
var prefix = 'textBox';
for(var i = 0; el = document.getElementById(prefix + i); i++){
if (el != ""){
var textToWrite = document.getElementById("el").value;
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
}
}
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null)
{
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else
{
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}
function loadFileAsText()
{
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("inputTextToSave").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
</script>
<input type=reset value=Clear id="reset">
</body>
</html>
可以选择所有元素为document.querySelectorAll('[id^=textbox]')
在上面查询返回的NodeList上迭代。
代码var textBoxes = document.querySelectorAll('[id^=textbox]');
var textToWrite;
for(var i in textBoxes){
textToWrite = textBoxes[i].value;
/* do your thing */
}
相关文章:
- Javascript:表单验证getElementById仅返回第一个id元素
- 如何为每个带有数字的id元素使用Javascript函数
- 使用regex选择ID元素
- Bootstrap Navbar在选择ID元素后未折叠
- JavaScript-更改一组包含特定文本和变量ID元素的链接
- 父页面中的 JQuery 未在部分视图中检测到 id 元素
- ID 元素在作为参数给出时返回
- 当 Javascript 中的 POST ID 元素插入 PDO 时,数据库中没有结果
- 为什么变量值不显示在 id 元素中
- 如何查找 id 元素的文本值
- 使用 ajax 将 id 元素传递给数据库
- 从另一个HTML文件中获取ID元素
- 正在克隆html标记id元素
- 如何在html中循环使用ID元素
- 获取动态生成的id元素的内部文本发送到谷歌分析
- 如何通过tagname检查ID元素
- jQuery滚动到底部的ID元素
- 检查Id元素javascript
- 在我显示一个id元素后,页面跳转到顶部
- 如何在HTML中突出显示当前的id元素