textarea的javascript内部文本不会重置重新加载页面 [英] javascript innertext of textarea doesn't reset realoading page
问题描述
我有以下代码在加载页面时完美运行
它允许双击 P 并使其可使用 textarea 进行编辑,并在单击保存"按钮时将其恢复为 P
双击some lorem ipsum"字符串会将 P 切换到 texarea,该内容取自 P 内容,因此它将显示some lorem ipsum"
你做了一些编辑,假设你删除了some lorem ipsum",你现在写新内容编辑",
当您单击保存"时,它会返回显示 P 哪些新内容现在是新内容编辑"
我遇到的问题是,如果我在 Firefox 中单击重新加载按钮,
P 回到原来的内容some lorem ipsum"
但是如果我双击some lorem ipsum"字符串,则不会从 P 中选择 textarea 文本
而是显示之前的新内容编辑"
我是不是忘记了什么?谢谢指教
var noteP = document.getElementById("noteP");var noteTA = document.getElementById("noteTA");var noteDD = document.getElementById("noteDD");var noteED = document.getElementById("noteED");var TestoOriginale = "";var TestoAttuale = "";noteED.style.display = "无";noteP.ondblclick = 函数 LoadText() {TestoOriginale = noteP.innerText;//加载 P 文本noteTA.innerText = TestoOriginale;//- 错误在这里 - 将 P 文本写入 textareanoteDD.style.display = "none";//隐藏 P DIVnoteED.style.display = "block";//显示文本区域 DIV}函数更新文本(){TestoAttuale = noteTA.value;//获取当前 textarea 文本noteP.innerText = TestoAttuale;//将其写入 PnoteDD.style.display = "block";noteED.style.display = "无";}document.getElementById("salvaBT").onclick = function AJAXPost(NoteUpdateForm) {更新文本();/**稍后添加更多代码**/}
<p id="noteP">一些lorem ipsum</span>.</p><div id="noteED"><form action="./note-update.php" method="post" id="NoteUpdateForm"><textarea id="noteTA" name="NoteUpdateText" form="NoteUpdateForm" rows="5" cols="40"></textarea><br></表单><input type="button" value="Cancel"><input type="button" id="salvaBT" value="Save" onclick="javascript:AJAXPost(this);">
据我所知,在事件发生后,您希望在保存更改后更新 textarea 值.
您需要将 value
传递给 textarea
以保存修改.
而不是做
noteTA.innerText = TestoOriginale;
做:
noteTA.value = TestoOriginale;
当您使用 innerText
时,您将文本放入 HTML 选择器中,而不是将其放入 textarea
value
属性中.
I have the following code that works perfectly when I load the page
it allows to double click a P and make it editable with textarea and revert it back to P when clicking the Save button
Double clicking the "some lorem ipsum" string will switch the P to a texarea which the content is taken from the P content so it will display "some lorem ipsum"
You make some edit , let say you delete the "some lorem ipsum" and you now write "new content edited",
when you click Save it turns back to showing the P which new content is now "new content edited"
The issue that I'm experiencing is that if I click the reload button in Firefox,
the P goes back to the original content "some lorem ipsum"
but if I doubleclick the "some lorem ipsum" string, the textarea text IS NOT PICKED from the P
instead it shows the previous "new content edited"
Am I forgetting something? Thank you for hinting
var noteP = document.getElementById("noteP");
var noteTA = document.getElementById("noteTA");
var noteDD = document.getElementById("noteDD");
var noteED = document.getElementById("noteED");
var TestoOriginale = "";
var TestoAttuale = "";
noteED.style.display = "none";
noteP.ondblclick = function LoadText() {
TestoOriginale = noteP.innerText; // load the P text
noteTA.innerText = TestoOriginale; // -THE ERROR IS HERE- write the P text into the textarea
noteDD.style.display = "none"; // hide P DIV
noteED.style.display = "block"; // show text area DIV
}
function UpdateText() {
TestoAttuale = noteTA.value; // get current textarea text
noteP.innerText = TestoAttuale; // write it into P
noteDD.style.display = "block";
noteED.style.display = "none";
}
document.getElementById("salvaBT").onclick = function AJAXPost(NoteUpdateForm) {
UpdateText();
/**
more code later
**/
}
<div id="noteDD">
<p id="noteP">some lorem ipsum</span>.</p>
</div>
<div id="noteED">
<form action="./note-update.php" method="post" id="NoteUpdateForm">
<textarea id="noteTA" name="NoteUpdateText" form="NoteUpdateForm" rows="5" cols="40"></textarea><br>
</form>
<input type="button" value="Cancel"><input type="button" id="salvaBT" value="Save" onclick="javascript:AJAXPost(this);">
</div>
As I understood, after the event you want to update textarea value after saving the changes.
You need to pass value
to textarea
to save the modification.
instead of doing
noteTA.innerText = TestoOriginale;
do:
noteTA.value = TestoOriginale;
When you use innerText
you put text inside the <textarea>
HTML selector instead of puting it into the textarea value
attribute.
这篇关于textarea的javascript内部文本不会重置重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!