简体   繁体   English

如何在 textarea 中显示断线和从 JavaScript 到我的 textarea 内部文本的数据提取

[英]How to show breaklines in textarea and data fetch from JavaScript to My inner Text of textarea

I want to get data from user in <textarea> and show it on the DOM page with edit button and with breaklines if user click on edit user old data send to new <textarea> with breaklines我想从<textarea>中的用户那里获取数据,并在带有编辑按钮的 DOM 页面上显示它,如果用户单击编辑用户旧数据发送到带有断线的新<textarea>

Adding an item.添加一个项目。

<form name="add" method="POST" action="{% url 'notepad:addnote' %}">{% csrf_token %}
<div class="modal-body">
    Title : <input name="title" class="w-50" type="text" required><br>
    Note :<br>
    <textarea  name="text" id="" cols="60" rows="10" required></textarea>
</div>
    <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
      <button type="submit" class="btn btn-primary">Save</button>
</div>

Showing an items.显示一个项目。

    <div id="content">
       {% for items in allitems %}
          <ul class="list-group d-inline-block my-4 ">
          <li class="list-group-item list-group-item-info  list-group-item-action {{items.id}}" aria-current="true">{{items.title}}<button onclick="editContent({{items.id}})" data-bs-toggle="modal" data-bs-target="#update"  type="button" class="save mx-2"><i class="fa fa-pencil-square-o"></i></button></li>
          <li class="list-group-item list-group-item-action {{items.id}}">{{items.text}}</li>
          </ul>
       {% endfor %}
   </div>

But if i show an items it removes break lines.但是,如果我显示一个项目,它会删除断线。 So I'll try pre tag to show that content with breaklines.所以我会尝试使用 pre 标签来显示带有断线的内容。

<div id="content"> 
 {% for items in allitems %}
     <ul class="list-group d-inline-block my-4 ">
       <li class="list-group-item list-group-item-info  list-group-item-action {{items.id}}" aria-current="true">{{items.title}}<button onclick="editContent({{items.id}})" data-bs-toggle="modal" data-bs-target="#update"  type="button" class="save mx-2"><i class="fa fa-pencil-square-o"></i></button></li>
       <pre><li class="list-group-item list-group-item-action {{items.id}}">{{items.text}}</li></pre>
     </ul>
 {% endfor %}
</div>

It shows break lines, but i again want to edit and send this content to my input and textarea to edit that's content, So i do that with JavaScript.它显示了断线,但我再次想编辑并将此内容发送到我的输入和 textarea 以编辑该内容,因此我使用 JavaScript 进行此操作。 And my place that i want to get data is我想获取数据的地方是

<div class="modal-body">
 Title : <input name="title" class="w-50" type="text" id="update_title" required value=""><br>
 Note :<br>
 <textarea name="text" cols="60" rows="10" id="update_text" required></textarea>

My JavaScript code is我的 JavaScript 代码是

function editContent(cls){
content = document.getElementsByClassName(`${cls}`);
oldtitle = content[0].innerText;
editeTitlePlace = document.getElementById('update_title');
editeTitlePlace.value = oldtitle

editTextPlace = document.getElementById('update_text');
oldtext = content[1].innerText;
newstr = ""

for(i=0;i < oldtext.length ;){
 char = oldtext.charAt(i);
 if (char == "\n"){
   newstr += " ";
  }
  else{
    newstr += char;
  }
  i++;
}
editeTextPlace.innerText = newstr
}

It shows my content,and replace "\n" with " " but if i do to add "\n" or "\n\t" it again shows me spaces not breaks a line in textarea.它显示了我的内容,并将“\n”替换为“”,但如果我添加“\n”或“\n\t”,它会再次向我显示空格,而不是在 textarea 中换行。 But i want that it shows me breakline in textarea.但我希望它在 textarea 中显示我的断线。

Add white-space: pre-wrap styling to the place you are showing the items.white-space: pre-wrap样式添加到您显示项目的位置。 This is preserve line breaks if they are found in textarea .如果在textarea中找到它们,则保留换行符。

Your code should look something like this.您的代码应该看起来像这样。

 <div id="content" style="white-space: pre-wrap;">
       {% for items in allitems %}
          <ul class="list-group d-inline-block my-4 ">
          <li class="list-group-item list-group-item-info  list-group-item-action {{items.id}}" aria-current="true">{{items.title}}<button onclick="editContent({{items.id}})" data-bs-toggle="modal" data-bs-target="#update"  type="button" class="save mx-2"><i class="fa fa-pencil-square-o"></i></button></li>
          <li class="list-group-item list-group-item-action {{items.id}}">{{items.text}}</li>
          </ul>
       {% endfor %}
   </div>

NOTE: You can add the mentioned css tag where needed.注意:您可以在需要的地方添加提到的css标签。

In my JavaScript While i using for loop i choose \n to replace with <br> in forloop and newstr add as <textarea> innerHTML and my <br> will change into break line in <textarea>在我的 JavaScript 中,当我使用 for 循环时,我选择\n替换 forloop 中的<br>newstr添加为<textarea> innerHTML,我的<br>将变为<textarea>中的断线

function editContent(cls){
content = document.getElementsByClassName(`${cls}`);
oldtitle = content[0].innerText;
editeTitlePlace = document.getElementById('update_title');
editeTitlePlace.value = oldtitle

editTextPlace = document.getElementById('update_text');
oldtext = content[1].innerText;
newstr = ""

for(i=0;i < oldtext.length ;){
 char = oldtext.charAt(i);
 if (char == "\n"){
   newstr += " ";
  }
  else{
    newstr += char;
  }
  i++;
}
editeTextPlace.innerHTML = newstr
}

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

相关问题 如何使用按钮从文本textarea数组中获取数据 - how to fetch data from an array of text textarea with a button using that button 如何使用javascript从textarea中删除我的数据? - How can I erase my data from textarea using javascript? 如何在* .jsp中显示文本到textArea? - How to show text to textArea in *.jsp? 从按钮到TextArea Javascript的文本 - Text from button to TextArea Javascript 在javascript / jQuery中的textarea中显示新打印的文本 - Show newly printed text in textarea in javascript/jQuery 从 TextArea 中删除文本并使用 javascript 将新数据插入其中 - Remove text from TextArea and insert new data into it using javascript 如何在另一个文本区域中显示文本区域和可编辑 div 中突出显示的文本的 position? - How to show the position of highlighted text in a textarea and editable div, in a another textarea? 如何从两个 input=“text” 和 textarea 中获取数据,然后将它们显示在里面<li></li> - How can I take data's from two's input=“text” and textarea then show them inside <li> 如何从textarea打印文本? - How to print text from textarea? 给定从asp:textbox生成的textarea,内部文本无法使用Javascript或Jquery进行“设置” - Given a textarea generated from asp:textbox, the inner text cannot be 'set' with Javascript or Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM