javascript 如何循环遍历 html 中的 ID 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33299495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:19:55  来源:igfitidea点击:

How to loop through ID elements in html

javascripthtml

提问by Niall_Maher

I'm trying to loop through all ID fields in my html and print them to a text field using JavaScript. This is what I have: Thanks for you help :-)
** It looks like most of your question is code, please give more details**

我正在尝试遍历 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>

回答by joyBlanks

You can select all the elements as document.querySelectorAll('[id^=textbox]')

您可以选择所有元素作为 document.querySelectorAll('[id^=textbox]')

Iterate it over the NodeList that the above query returns.

在上述查询返回的 NodeList 上迭代它。

Code

代码

var textBoxes = document.querySelectorAll('[id^=textbox]');
var textToWrite;
for(var i in textBoxes){
   textToWrite = textBoxes[i].value;
   /* do your thing */
}