javascript - 如何从所选文本中获取相邻字符?

标签 javascript jquery

我有这样一个字符串:

var comment = 'this is a test';

假设选择了this i,现在我需要null(左侧)和s(右侧)。我怎样才能得到它们?


我可以获得这样的选定文本:

function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}

var selected_text = getSelectionHtml();

最佳答案

document.addEventListener("click", function() {
  var selection = window.getSelection();
  // Check if there are any ranges selected.
  if (selection.rangeCount > 0 && selection.type == "Range") {
    // Text content of the element.
    var text = selection.anchorNode.textContent;
    // selection.anchorOffset is the start position of the selection
    var before = text.substring(selection.anchorOffset-1, selection.anchorOffset);
    // selection.extentOffset is the end position of the selection
    var after = text.substring(selection.extentOffset, selection.extentOffset+1);
    // Check if there are any letters before or after selected string.
    // If not, change before and after to null.
    before = before.length === 1 ? before : null;
    after = after.length === 1 ? after : null;
    console.log(before, after);
  }
});
<div>this is a test</div>

获取两个字符:

document.addEventListener("click", function() {
  var selection = window.getSelection();
  // Check if there are any ranges selected.
  if (selection.rangeCount > 0 && selection.type == "Range") {
    // Text content of the element.
    var text = selection.anchorNode.textContent;
    // selection.anchorOffset is the start position of the selection
    var before = text.substring(selection.anchorOffset-2, selection.anchorOffset);
    // selection.extentOffset is the end position of the selection
    var after = text.substring(selection.extentOffset, selection.extentOffset+2);
    // Check if there are any letters before or after selected string.
    // If not, change before and after to null.
    before = before.length >= 1 ? before : null;
    after = after.length >= 1 ? after : null;
    console.log(before, after);
  }
});
<div>this is a test</div>

关于javascript - 如何从所选文本中获取相邻字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34844005/

相关文章:

javascript - jQuery RightClick 处理程序插件

php - 如何在页面加载时使用 ajax 将 php include 加载到页面中

javascript - jQuery |无法获取事件选项卡的 href

jquery - jqgrid隐藏子网格

jquery - Fancybox 打开文本文件?

javascript - 如何存储 DIV 值? (鹅毛笔)

javascript - `Uncaught TypeError: Cannot read property ' 退出 ' of undefined`

javascript - Backbone Collections - 无法使用 set 方法设置 'rank'

jquery - 获取单选按钮的检查状态

javascript - 如何使用控制台日志到 ajax 参数?