Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript文本更改未在HTML内部修复;至于;_Javascript_Html_Dom - Fatal编程技术网

Javascript文本更改未在HTML内部修复;至于;

Javascript文本更改未在HTML内部修复;至于;,javascript,html,dom,Javascript,Html,Dom,我有一个课程项目,我有一个帖子列表,为发布的用户显示一个按钮或链接来编辑帖子。我需要用旧文本替换文本区域的旧帖子,编辑文本,保存并显示新帖子。 最大的问题是,当我点击编辑按钮时,我看到文本变为文本区域,但它不会停留(一两秒钟),而是在我可以更改帖子内容之前消失。这篇旧帖子看起来好像什么都没发生。有没有人能给我一点建议 Javascript function editpost(postid, page){ alert("edit link clicked"); co

我有一个课程项目,我有一个帖子列表,为发布的用户显示一个按钮或链接来编辑帖子。我需要用旧文本替换文本区域的旧帖子,编辑文本,保存并显示新帖子。 最大的问题是,当我点击编辑按钮时,我看到文本变为文本区域,但它不会停留(一两秒钟),而是在我可以更改帖子内容之前消失。这篇旧帖子看起来好像什么都没发生。有没有人能给我一点建议

Javascript

function editpost(postid, page){
   alert("edit link clicked");
   const oldtext = document.getElementById(`${postid}`).innerHTML;
   const newtext = document.getElementById(`${postid}`);

   newtext.innerHTML = `
     <textarea id=${postid} > ${oldtext} </textarea>
     <button id="change-${postid}" class="btn btn-success" onclick="changepost(${postid}, ${page})" 
             type="submit" style="width:30%;">Change</button>
     <button id="cancel-${postid}" class="btn btn-danger"  onclick="cancel()" 
             type="cancel"style="width:30%;">Cancel</button>
   `;
}
功能编辑帖子(posted,第页){
警报(“单击编辑链接”);
constoldtext=document.getElementById(`${postid}').innerHTML;
const newtext=document.getElementById(`${postid}`);
newtext.innerHTML=`
${oldtext}
改变
取消
`;
}
HTML

   {%for Post in page_obj %}
        <div class="container" >
             <div class="row">
                 <div class="col-lg-4" style="text-align: center;">
                    <p> {{ Post.User |title }} </p>
                
                    {% if UserEdit|title == Post.User|title %}
                         <p>
                         <a href="" id="edit-{{Post.id}}" onclick="editpost({{Post.id}},'profile')"> 
                             Edit Post
                         </a></p>
                    {% endif %}
                 </div>
                 <div class="col-lg-8" style="text-align: center;">

                        <div id="post-{{Post.id}}"  >
                            <p id={{Post.id}}> {{Post.Post}} </p>
                        </div>

                 </div>
             </div>
        </div>
   
    {% endfor %}

{%用于第_obj%页中的帖子]
{{Post.User | title}}

{%if UserEdit | title==Post.User | title%}

{%endif%}

{{Post.Post}

{%endfor%}

感谢所有高级用户的帮助。

您需要将锚定标签替换为按钮标签

{% if UserEdit|title == Post.User|title %}
<p>
<button id="edit-{{Post.id}}" onclick="editpost({{Post.id}},'profile')">
Edit Post</button>
</p>
{% endif %}
{%if UserEdit | title==Post.User | title%}

编辑文章

{%endif%}
表单中没有
类型的按钮
默认为
type=“submit”
。您没有阻止默认表单提交,因此页面将重新加载。尝试将
type=“button”
添加到不用于提交的按钮。另外,还要做一些关于防止默认表单提交和防止默认事件操作的研究。谢谢,您的评论和答案结合起来是有效的!那为什么有用呢?谢谢!你的回答和评论结合起来很有效!