如何使用 Javascript 获取跨度选定部分

发布于 2024-08-08 02:21:39 字数 44 浏览 11 评论 0原文

有没有办法在跨度上获取突出显示的选择开始和选择长度?

谢谢

is there a way to get the highlighted selectionstart and selectedlenght on a span ?

Thanks

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

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

发布评论

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

评论(3

梦与时光遇 2024-08-15 02:21:39

我在 IERange 中使用了我自己的算法优化,它提供了 IE TextRange 的包装器(这是您从 IE 中的选择中获得的结果),为它们提供与 DOM Range 相同的界面。

要获取文档中的选择,请使用类似以下内容的内容:

var sel = window.getSelection(); // Provided by IERange in IE, built-in in other browsers
var range = sel.getRangeAt(0); // Note this doesn't work in Safari 2

range 现在具有属性 startContainerstartOffset,它们分别是对节点和该节点内代表选择开始的偏移量,以及代表选择结束的相应属性 endContainerendOffset

I use my own optimization of the algorithms in IERange, which provides a wrapper around IE's TextRange (which is what you get from the selection in IE) to give them the same interface as DOM Ranges.

To get the selection in the document, use something like the following:

var sel = window.getSelection(); // Provided by IERange in IE, built-in in other browsers
var range = sel.getRangeAt(0); // Note this doesn't work in Safari 2

range now has properties startContainer and startOffset, which are respectively a reference to a node and an offset within that node that represent the start of the selection, and corresponding properties endContainer and endOffset that represent the end of the selection.

我ぃ本無心為│何有愛 2024-08-15 02:21:39

尝试一下这个:

  var span = document.getElementById('span1');
  if (document.selection) { //IE
      var bm = document.selection.createRange().getBookmark();
      var sel = span.createTextRange();
      sel.moveToBookmark(bm);

      var sleft = span.createTextRange();
      sleft.collapse(true);
      sleft.setEndPoint("EndToStart", sel);
      span.selectionStart = sleft.text.length
      span.selectionEnd = sleft.text.length + sel.text.length;
      span.selectedText = sel.text;
  }
  else if (span.selectionStart){ //FF
     span.selectedText = span.substring(span.selectionStart,span.selectionEnd);
  }

  alert("Selection Start==> " + span.selectionStart + "\n" +
     "Selection End  ==> " + span.selectionEnd + "\n" +
     "Selected Text  ==> " + span.selectedText + "\n" +
     "TextArea Value ==> " + span.value);

Have a try on this:

  var span = document.getElementById('span1');
  if (document.selection) { //IE
      var bm = document.selection.createRange().getBookmark();
      var sel = span.createTextRange();
      sel.moveToBookmark(bm);

      var sleft = span.createTextRange();
      sleft.collapse(true);
      sleft.setEndPoint("EndToStart", sel);
      span.selectionStart = sleft.text.length
      span.selectionEnd = sleft.text.length + sel.text.length;
      span.selectedText = sel.text;
  }
  else if (span.selectionStart){ //FF
     span.selectedText = span.substring(span.selectionStart,span.selectionEnd);
  }

  alert("Selection Start==> " + span.selectionStart + "\n" +
     "Selection End  ==> " + span.selectionEnd + "\n" +
     "Selected Text  ==> " + span.selectedText + "\n" +
     "TextArea Value ==> " + span.value);
失与倦" 2024-08-15 02:21:39

您可能会在此quirksmode 帖子中找到一些答案:

You might find some answers in this quirksmode post:

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