如何使用javascript按字母顺序对textarea标签进行排序并在另一个textarea标签中

我想知道如何按字母顺序对标签的内容<textarea>进行排序,然后<textarea>使用 javascript 将其输出到另一个第二个标签中。

StackOverflow 上有一些与之前提出的问题类似的问题,但我认为他们的任何答案都不能应用于我下面的代码。

这是我的代码:

.con {

    display: flex; 

    margin-top: 2px;

    margin-left: 20px;

}


.button {

    background: #4CAF50;

    border: none;

    outline: none;

    color: #ffffff;

    padding: 14px;

    height: 60px;

    width: 140px;

    border-radius: 0 10px;

    margin-top: 0px;

    font-size: 22px;

    cursor: pointer;

}


.txt {

    display: flex; 

    margin-right: 20px;

    background: #ffffff;

    border: 0;

    outline: none;

    height: 700px;

    width: 45%;

    border-radius: 10px;

    box-shadow: 0 4px 8px 0 rgba(141, 105, 105, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);

    margin-top: 0px;

}


.text {

    border: none;

    margin-top: 18px;

    margin-left: 18px;

    height: 660px;

    width: 630px;

    outline: none;

    font-size: 22px;

    resize: none;

}


.asci {

    background: #ffffff;

    border: 0;

    outline: none;

    height: 700px;

    width: 45%;

    border-radius: 10px;

    box-shadow: 0 4px 8px 0 rgba(141, 105, 105, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);

}


.ascii {

    border: none;

    margin-top: 20px;

    margin-left: 10px;

    height: 660px;

    width: 640px;

    outline: none;

    font-size: 22px;

    resize: none;

}


浮云间
浏览 62回答 4
4回答

斯蒂芬大帝

首先,我首先将元素split()的值textarea放入数组中://split the value on a space characterlet wordsArr = document.querySelector('#input').value.split(' ');然后对数组进行排序:wordsArr.sort((a, b) => {&nbsp; &nbsp; const word1 = a.toUpperCase();&nbsp; &nbsp; const word2 = b.toUpperCase();&nbsp; &nbsp; if (word1 < word2) {&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; if (word2 > word1) {&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }&nbsp; &nbsp; /* if neither of those if statements fire, that means the words are the&nbsp;&nbsp; &nbsp; same and can stay in the same position */&nbsp; &nbsp; return 0;};然后将数组元素连接回单个字符串,并将其设置为其他文本区域的值:document.querySelector('#output').value = wordsArr.join(' ');

呼啦一阵风

我也会从分裂开始,但我们不要重新发明世界。您可以在 4 行函数中使用 js 数组排序、toString 和替换方法function myFunction(){&nbsp; &nbsp;var text = document.getElementById('input').value;&nbsp; &nbsp;var textArray = text.split(" ").sort();&nbsp; &nbsp;var output= document.getElementById('output');&nbsp; &nbsp;output.value = textArray.toString().replace(/,/g," ");}.con {&nbsp; &nbsp; display: flex;&nbsp;&nbsp; &nbsp; margin-top: 2px;&nbsp; &nbsp; margin-left: 20px;}.button {&nbsp; &nbsp; background: #4CAF50;&nbsp; &nbsp; border: none;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; color: #ffffff;&nbsp; &nbsp; padding: 14px;&nbsp; &nbsp; height: 60px;&nbsp; &nbsp; width: 140px;&nbsp; &nbsp; border-radius: 0 10px;&nbsp; &nbsp; margin-top: 0px;&nbsp; &nbsp; font-size: 22px;&nbsp; &nbsp; cursor: pointer;}.txt {&nbsp; &nbsp; display: flex;&nbsp;&nbsp; &nbsp; margin-right: 20px;&nbsp; &nbsp; background: #ffffff;&nbsp; &nbsp; border: 0;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; height: 700px;&nbsp; &nbsp; width: 45%;&nbsp; &nbsp; border-radius: 10px;&nbsp; &nbsp; box-shadow: 0 4px 8px 0 rgba(141, 105, 105, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);&nbsp; &nbsp; margin-top: 0px;}.text {&nbsp; &nbsp; border: none;&nbsp; &nbsp; margin-top: 18px;&nbsp; &nbsp; margin-left: 18px;&nbsp; &nbsp; height: 660px;&nbsp; &nbsp; width: 630px;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; font-size: 22px;&nbsp; &nbsp; resize: none;}.asci {&nbsp; &nbsp; background: #ffffff;&nbsp; &nbsp; border: 0;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; height: 700px;&nbsp; &nbsp; width: 45%;&nbsp; &nbsp; border-radius: 10px;&nbsp; &nbsp; box-shadow: 0 4px 8px 0 rgba(141, 105, 105, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);}.ascii {&nbsp; &nbsp; border: none;&nbsp; &nbsp; margin-top: 20px;&nbsp; &nbsp; margin-left: 10px;&nbsp; &nbsp; height: 660px;&nbsp; &nbsp; width: 640px;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; font-size: 22px;&nbsp; &nbsp; resize: none;}<html><head>&nbsp; &nbsp; <title>alphabetical order machine</title>&nbsp; &nbsp; <link rel="stylesheet" href="ascii.css"></head><body>&nbsp; &nbsp; <div class="con">&nbsp; &nbsp; <form class="txt">&nbsp; &nbsp; &nbsp; &nbsp; <textarea class="text"&nbsp; id="input" type="text" placeholder="type your text here"></textarea>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <input class="button" type='button' value="alphabetize" onclick="myFunction()">&nbsp; &nbsp; </form>&nbsp; &nbsp; <form class="asci">&nbsp; &nbsp; &nbsp; &nbsp; <textarea class="ascii" id="output" type="text" placeholder="your alphabetized text will appear here"></textarea>&nbsp; &nbsp; </form>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script src="ascii.js"></script></body></html>

PIPIONE

注册“输入”和“点击”事件以形成。为每个事件创建一个事件处理程序。document.forms.ID.oninput = inputHandler;document.forms.ID.onclick = clickHandler;创建事件处理程序时,传递事件对象function inputHandler(e) {...定义引用的变量:所有表单字段的 NodeList/*&nbsp;- "this" is the form tag- .elements is a property that collects all form type tags*/const field = this.elements;用户与之互动的标签&nbsp;/*&nbsp;- e.target property always points to the tag a user has clicked, changed,&nbsp; &nbsp;&nbsp; &nbsp;entered data upon, etc.*/const input = e.target;任何其他相关标签,例如输出/*- Any input, output, textarea, etc you want to access just prefix the NodeList&nbsp; identifier (see first variable) to any form type tag #id or [name]*/const output = field.output;接下来,定义一个条件来确定您要处理哪个标签(通常是e.target)而不是其他任何内容。通过排除其他不必要的标签,您可以完全控制做什么以及如何完成。if (e.target.id === 'input') {.../* OR */if (e.target.className === 'button') {...演示const form = document.forms.editor;form.oninput = copyText;form.onclick = sortText;function copyText(e) {&nbsp; const ui = this.elements;&nbsp; const inp = e.target;&nbsp; const out = ui.output;&nbsp; if (inp.id === 'input') {&nbsp; &nbsp; out.value = inp.value;&nbsp; }}function sortText(e) {&nbsp; const ui = this.elements;&nbsp; const btn = e.target;&nbsp; const out = ui.output;&nbsp; if (btn.className === "button") {&nbsp; &nbsp; let result = out.value.split(' ').sort((a, b) => a - b);&nbsp; &nbsp; out.value = result;&nbsp; }}.box {&nbsp; display: flex;&nbsp; flex-flow: column nowrap;&nbsp; justify-content: center;&nbsp; align-items: center;}textarea {&nbsp; width: 90vw;&nbsp; height: 30vw;}.button {&nbsp; display: block;&nbsp; width: 90vh;&nbsp; height: 5vw;&nbsp; margin: 5px auto;}<!DOCTYPE html><html><head>&nbsp; <meta charset='utf-8'>&nbsp; <title></title></head><body>&nbsp; <form id="editor">&nbsp; &nbsp; <fieldset>&nbsp; &nbsp; &nbsp; <textarea id="input" placeholder="Enter a space-delimited list of items here"></textarea>&nbsp; &nbsp; &nbsp; <button class="button" type='button'>Sort</button>&nbsp; &nbsp; &nbsp; <textarea id="output" placeholder="Sorted list will be provided here"></textarea>&nbsp; &nbsp; </fieldset>&nbsp; </form></body></html>

潇湘沐

您需要使用split()和sort()。这是你的代码:function myFunction() {&nbsp; const input1 = document.getElementById("input");&nbsp; const input2 = document.getElementById("output");&nbsp;&nbsp;&nbsp; let content = input1.value; //Get its content&nbsp; var array = content.split(" "); //And replace each space by a comma to make an array.&nbsp;&nbsp;&nbsp; input2.value = array.sort();&nbsp; &nbsp; //alphabetize it!&nbsp; input2.value = input2.value.replace(",", " "); //Restore the spaces.}.con {&nbsp; &nbsp; display: flex;&nbsp;&nbsp; &nbsp; margin-top: 2px;&nbsp; &nbsp; margin-left: 20px;}.button {&nbsp; &nbsp; background: #4CAF50;&nbsp; &nbsp; border: none;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; color: #ffffff;&nbsp; &nbsp; padding: 14px;&nbsp; &nbsp; height: 60px;&nbsp; &nbsp; width: 140px;&nbsp; &nbsp; border-radius: 0 10px;&nbsp; &nbsp; margin-top: 0px;&nbsp; &nbsp; font-size: 22px;&nbsp; &nbsp; cursor: pointer;}.txt {&nbsp; &nbsp; display: flex;&nbsp;&nbsp; &nbsp; margin-right: 20px;&nbsp; &nbsp; background: #ffffff;&nbsp; &nbsp; border: 0;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; height: 700px;&nbsp; &nbsp; width: 45%;&nbsp; &nbsp; border-radius: 10px;&nbsp; &nbsp; box-shadow: 0 4px 8px 0 rgba(141, 105, 105, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);&nbsp; &nbsp; margin-top: 0px;}.text {&nbsp; &nbsp; border: none;&nbsp; &nbsp; margin-top: 18px;&nbsp; &nbsp; margin-left: 18px;&nbsp; &nbsp; height: 660px;&nbsp; &nbsp; width: 630px;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; font-size: 22px;&nbsp; &nbsp; resize: none;}.asci {&nbsp; &nbsp; background: #ffffff;&nbsp; &nbsp; border: 0;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; height: 700px;&nbsp; &nbsp; width: 45%;&nbsp; &nbsp; border-radius: 10px;&nbsp; &nbsp; box-shadow: 0 4px 8px 0 rgba(141, 105, 105, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);}.ascii {&nbsp; &nbsp; border: none;&nbsp; &nbsp; margin-top: 20px;&nbsp; &nbsp; margin-left: 10px;&nbsp; &nbsp; height: 660px;&nbsp; &nbsp; width: 640px;&nbsp; &nbsp; outline: none;&nbsp; &nbsp; font-size: 22px;&nbsp; &nbsp; resize: none;}<head>&nbsp; &nbsp; <title>alphabetical order machine</title></head><body>&nbsp; &nbsp; <div class="con">&nbsp; &nbsp; <form class="txt">&nbsp; &nbsp; &nbsp; &nbsp; <textarea class="text"&nbsp; id="input" type="text" placeholder="type your text here"></textarea>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <input class="button" type='button' value="alphabetize" onclick="myFunction();">&nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; <form class="asci">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <textarea class="ascii" id="output" type="text" placeholder="your alphabetized text will appear here"></textarea>&nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </div></body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5