简体   繁体   English

页面div元素更新后,所选文本消失

[英]Selected text disappears after page div element update

I have created a chat page. 我已经创建了一个聊天页面。 Problem is when I select chat text and chat refreshes, the selected text disappears. 问题是当我选择聊天文本并刷新聊天时,所选文本消失了。 I know there are chats that keep selected text even if the page refreshes. 我知道即使页面刷新了,聊天也会保留选定的文本。 I want to keep text selected after updating text. 我想在更新文本后保持选中状态。 My page has div element and it updates its content from chatLog.txt file. 我的页面具有div元素,它从chatLog.txt文件更新其内容。 Any hints how to make it work? 有任何提示如何使其工作吗?

$(function worker(){
    $.ajaxSetup ({
        cache: false,
        complete: function() {

          setTimeout(worker, 3000);

        }
    });
     $("#divBox").load("chatLog.txt");
});

Any help is appreciated, thanks :) 任何帮助表示赞赏,谢谢:)

That behavior is normal, if you refresh (re-mount) a part of the DOM, the selected text will disappear. 这种行为是正常的,如果刷新(重新安装)DOM的一部分,则所选文本将消失。 If you want to maintain the selected text, you should try React, Vue or Angular, because these handle virtual DOM, these technologies does easy make a chat app and no re-mount the DOM every moment. 如果您要保留选定的文本,则应尝试使用React,Vue或Angular,因为它们可以处理虚拟DOM,因此这些技术确实可以轻松地创建聊天应用程序,而不必每次都重新安装DOM。 Not use Ajax, you should work with websockets, use socket.io or Laravel Websocket, or similar. 不使用Ajax,您应该使用websockets,使用socket.io或Laravel Websocket或类似的东西。

Try :) 尝试:)

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

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