[英]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.