Javascript 如何在html中循环使用ID元素
我正在尝试遍历html中的所有ID字段,并使用JavaScript将它们打印到文本字段中。 这就是我所拥有的: 感谢您的帮助:-)Javascript 如何在html中循环使用ID元素,javascript,html,Javascript,Html,我正在尝试遍历html中的所有ID字段,并使用JavaScript将它们打印到文本字段中。 这就是我所拥有的: 感谢您的帮助:-) **看起来你的大部分问题都是代码,请提供更多细节** <html> <head> <title>Modifying Sentiment</title> </head> <body> <body bgcolor="#E6E6FA"> <font color=black size=
**看起来你的大部分问题都是代码,请提供更多细节**
<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>
改变情绪
改变情绪
要保存的文本:
添加肯定形容词:
要另存为的文件名:
将文本保存到文件
选择要加载的文件:
加载所选文件
函数saveTextAsFile()
{
var-el;
变量前缀='textBox';
for(var i=0;el=document.getElementById(前缀+i);i++){
如果(el!=“”){
var textToWrite=document.getElementById(“el”).value;
var textFileAsBlob=newblob([textToWrite],{type:'text/plain'});
var fileNameToSaveAs=document.getElementById(“inputFileNameToSaveAs”).value;
}
}
var downloadLink=document.createElement(“a”);
downloadLink.download=fileNameToSaveAs;
downloadLink.innerHTML=“下载文件”;
如果(window.webkitURL!=null)
{
downloadLink.href=window.webkitURL.createObjectURL(textFileAsBlob);
}
其他的
{
downloadLink.href=window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick=destroyClickedElement;
downloadLink.style.display=“无”;
document.body.appendChild(下载链接);
}
downloadLink.click();
}
函数销毁ClickedElement(事件)
{
document.body.removeChild(event.target);
}
函数loadFileAsText()
{
var fileToLoad=document.getElementById(“fileToLoad”).files[0];
var fileReader=newfilereader();
fileReader.onload=函数(fileLoadedEvent)
{
var textFromFileLoaded=fileloadevent.target.result;
document.getElementById(“inputTextToSave”).value=textFromFileLoaded;
};
readAsText(fileToLoad,“UTF-8”);
}
您可以选择所有元素作为document.querySelectorAll(“[id^=textbox]”)
在上述查询返回的节点列表上迭代它
代码
什么是ID字段?我想您已经纠正了所有我的ID元素都被调用的循环:textbox1、textbox2、textbox3、…签出-如果(el!=“”),则
的用途是什么el
是DOM元素,而不是字符串。
var textBoxes = document.querySelectorAll('[id^=textbox]');
var textToWrite;
for(var i in textBoxes){
textToWrite = textBoxes[i].value;
/* do your thing */
}