简体   繁体   English

根据 keyCode 将文本区域拆分为数组,并为每一行添加 li 标签 - JavaScript

[英]Split text area into an array based on keyCode and add li tags to each line - JavaScript

I am new to JavaScript so struggling to get this over the line.我是 JavaScript 的新手,所以很难通过电话。 I have a form with a textarea field.我有一个带有 textarea 字段的表单。 After each keypress event of the enter key I need each line to get wrapped in li tags.在回车键的每个按键事件之后,我需要将每一行都包裹在 li 标签中。 Then once submitted the data is outputted as a list.然后一旦提交,数据将作为列表输出。 I am using Javascript not JQuery. The problem is when I add the li tags the opening tag is always added to the first item and ends up having multiple opening li tags and the closing tags work as they should.我使用的是 Javascript,而不是 JQuery。问题是当我添加 li 标签时,开始标签总是添加到第一项,最终有多个开始 li 标签,结束标签正常工作。

I have tried this so far.到目前为止我已经试过了。 HTML HTML

<textarea id="form__comments" name="comments" class="form__comments" rows="10" onkeypress="addLiTag(event);"></textarea>

JS JS

function addLiTag(e) {
  const commentValue = document.getElementById("form__comments").value
  var lines = commentValue.split(/\r?\n/)
  var key = e.keyCode

  // If the user has pressed enter
  if (key === 13) {

    document.getElementById("form__comments").value = "<li>" + lines + "</li>"

    return false
  } else {
    return true
  }
}

This is the output within the Textarea这是Textarea中的output

<li><li><li>One</li>,two</li>Three,</li>

So the main issue with your existing code is that when you add the value back to your textarea , you wrap an array (the lines variable) inside of an opening and closing <li> tag, rather than wrapping each line inside of its own tag.因此,现有代码的主要问题是,当您将值添加回textarea时,您将数组( lines变量)包装在开始和结束<li>标签内,而不是将每一行包装在其自己的标签内.

Also when adding the value back, I'm assuming you want to preserve the lines as well (meaning each <li> is also on its own line), in which case you also would need to add back the newline character after each line.此外,在添加回值时,我假设您也想保留这些行(意味着每个<li>也在其自己的行上),在这种情况下,您还需要在每行之后添加回换行符。

And lastly, to prevent duplicate <li> tags being added, the simplest thing is to just remove the tags and parse the value of your text area again (this also can prevent issues with editing previous lines).最后,为了防止添加重复的<li>标签,最简单的方法是删除标签并再次解析文本区域的值(这也可以防止编辑前几行时出现问题)。

 const addLiTag = e => { let lines = document.querySelector("#form__comments").value.replaceAll("<li>", "").replaceAll("</li>", "").split(/\r?\n/) if(e.keyCode === 13) document.querySelector("#form__comments").value = "<li>" + lines.join("</li>\n<li>") + "</li>" } document.querySelector("#form__comments").addEventListener("keypress", addLiTag)
 <textarea id="form__comments" name="comments" class="form__comments" rows="10"></textarea>

Notes笔记

I'm sure this could also be done with a single Regular Expression that splits on either a newline or the </li>\n<li> delimeter, rather than using two .replaceAll() calls, however I'm not that great at Regular Expressions.我确信这也可以通过在换行符或</li>\n<li>分隔符上拆分的单个正则表达式来完成,而不是使用两个.replaceAll()调用,但是我不是那么好在正则表达式。 But hopefully this answer demonstrates the concept of removing all of the <li> tags and re-parsing the value each time.但希望这个答案展示了删除所有<li>标签并每次重新解析值的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM