通过 JavaScript 在文本区域中保存和加载插入符位置

发布于 2024-10-07 06:55:04 字数 356 浏览 1 评论 0原文

我正在寻找一种方法来保存和加载 Web 应用程序中文本区域中的插入符位置,以便当用户重新打开 Web 应用程序时,它们会自动返回到他们离开的位置。

我已经看到了 jQuery 的 jCaret 插件,但由于我的 Web 应用程序不使用 jQuery,所以我需要可以在纯 JavaScript 中运行的东西。

另外,启动该功能以保存插入符位置的最佳方式是什么?我想到的第一个方法是在每次按键时重新保存它,但这似乎有点低效。我一直在考虑让应用程序通过 onBeforeUnload 事件保存位置,但如果您能想到更好的方法,请分享!

I'm looking for a method to save and load the caret position in a textarea in a web-application, so that when the user re-opens the web-application they are automatically taken back to the position they left it.

I've seen the jCaret plugin for jQuery, but as my web application does not use jQuery I need something that works in pure JavaScript.

Also, what would be the best way of initiating the function to save the caret's position? The first method that came to mind was re-saving it on every keypress, but this seems a little inefficient. I've been thinking of having the application save the position via the onBeforeUnload event, but if you can think of a better way please share!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-10-14 06:55:04

通过一些小的修改,您可以在不使用 jQuery 的情况下使用 jCaret。我查看了 jCaret 源代码,它使用 jQuery 完成两件事:使用 jQuery 选择器提供元素,以及测试浏览器是否为 IE。摆脱这些,你就可以上路了。

更详细的说明:

  1. 下载 jCaret 的未压缩源代码:http://examplet。 buss.hk/download/download.php?plugin=caret.1.02
  2. var caret; 添加到顶部。
  3. (function($,len,createRange,duplicate){ 中删除 $,
  4. $.fn 中删除 $.fn. .caret=function(options,opt2){
  5. var start,end,t=this[0],browser=$.browser.msie; 更改为 var start,end ,t=this[0],browser=/(msie) ([\w.]+)/.test(navigator.userAgent);
  6. 在最后一行中,删除 jQuery, from })(jQuery,"length","createRange","duplicate");

要使用它,您需要执行以下操作:

caret.call([document.getElementById("myText")], options, opt2);

With some minor modifications, you can use jCaret without jQuery. I had a look at the jCaret source, and it uses jQuery for all of two things: to supply the element using jQuery selectors, and to test if the browser is IE. Get rid of these and you're on your way.

More detailed instructions:

  1. Download the uncompressed source for jCaret: http://examplet.buss.hk/download/download.php?plugin=caret.1.02
  2. Add var caret; to the top.
  3. Remove the $, from (function($,len,createRange,duplicate){
  4. Remove $.fn. from $.fn.caret=function(options,opt2){
  5. Change var start,end,t=this[0],browser=$.browser.msie; to var start,end,t=this[0],browser=/(msie) ([\w.]+)/.test(navigator.userAgent);
  6. In the very last line, remove jQuery, from })(jQuery,"length","createRange","duplicate");

To use it, you need to do:

caret.call([document.getElementById("myText")], options, opt2);
流绪微梦 2024-10-14 06:55:04

经过进一步研究,我找到了一个使用 HTML5 localStorage 的简单解决方案。

这是我为保存插入符号位置而编写的脚本:

function caretPositionSave() { 
    window.localStorage.setItem("CaretPosition", document.querySelector("#editor").selectionStart);
};

另一个用于加载它的脚本:

function caretPositionLoad() {
    document.querySelector("#editor").focus();
    if (localStorage.CaretPosition) {
        document.querySelector("#editor").selectionStart = localStorage.CaretPosition;
    };
};

这些与设置屏幕滚动位置的类似函数相结合,似乎完美地完成了这一任务!

After further research, I've come to a simple solution that uses HTML5 localStorage.

Here's the script I made for saving the caret position:

function caretPositionSave() { 
    window.localStorage.setItem("CaretPosition", document.querySelector("#editor").selectionStart);
};

And another one for loading it:

function caretPositionLoad() {
    document.querySelector("#editor").focus();
    if (localStorage.CaretPosition) {
        document.querySelector("#editor").selectionStart = localStorage.CaretPosition;
    };
};

These, combined with similar functions to set the screen's scroll position, seem to do the trick perfectly!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文