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

Javascript 如何从所选文本中获取相邻字符?,javascript,jquery,Javascript,Jquery,我有这样一个字符串: var comment = 'this is a test'; function getSelectionHtml() { var html = ""; if (typeof window.getSelection != "undefined") { var sel = window.getSelection(); if (sel.rangeCount) { var container = docum

我有这样一个字符串:

var comment = 'this is a test';
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();
假设选择了
this i
,现在我需要
null
(左侧)和
s
(右侧)。我怎样才能得到它们


我可以获得如下所示的选定文本:

var comment = 'this is a test';
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();
函数getSelectionHtml(){ var html=“”; if(typeof window.getSelection!=“未定义”){ var sel=window.getSelection(); 如果(选择范围计数){ var container=document.createElement(“div”); 对于(变量i=0,len=sel.rangeCount;i
document.addEventListener(“单击”,函数(){
var selection=window.getSelection();
//检查是否选择了任何范围。
如果(selection.rangeCount>0&&selection.type==“范围”){
//元素的文本内容。
var text=selection.anchorNode.textContent;
//selection.AnchoroOffset是选择的开始位置
var before=text.substring(selection.anchorOffset-1,selection.anchorOffset);
//selection.extendtoffset是选择的结束位置
var after=text.substring(selection.extendtoffset,selection.extendtoffset+1);
//检查所选字符串前后是否有字母。
//如果不是,则将前后更改为null。
before=before.length==1?before:null;
after=after.length==1?after:null;
console.log(之前、之后);
}
});

这是一个测试
您不需要jQuery。您只需要
window.getSelection()
。这将返回一个对象。您可以使用
window.getSelection().anchorNode.data
获取周围的文本,并使用
window.getSelection().anchorOffset
获取该文本中所选内容的索引。综上所述,我们有

var selection       = window.getSelection();
var selectionText   = selection.toString();
var surroundingText = selection.anchorNode.data;
var index           = selection.anchorOffset;

var leftNeighbor  = surroundingText[index - 1];
var rightNeighbor = surroundingText[index + selectionText.length];
请注意,当没有相邻字符时,您将获得
未定义的
,而不是
null

window.addEventListener(“单击”,函数(){
var selection=window.getSelection();
var selectionText=selection.toString();
var surroundingText=selection.anchorNode.data;
var指数=selection.anchorooffset;
var leftNeighbor=surroundingText[index-1];
var rightnearch=surroundingText[index+selectionText.length];
警报(左邻居+“”+右邻居);
});

这是一个测试

我不太明白你的问题。您已经展示了一个将文本选择为JS字符串的示例,但是使用从HTML中选择的代码?如果您从文本中选择
”,结果应该是什么?@Teemu是的,我的示例不太清楚。。。!我的意思是从HTML中选择。实际上,我正在尝试为标记编辑器创建一个工具栏。我希望当用户单击
I
图标时,检查相邻字符,如果它们是
*
,则删除它们,否则在选定文本周围附加它们。请告诉我如何为两个相邻字符扩展脚本?如下所示:所选文本:
a te
。L:
s{space}
,R:
st
@Shafizadeh只需将
-1
更改为
-2
+1
更改为
+2
,以及
==1
更改为
=1
。我对这个解决方案有一个奇怪的问题。当我选择某个字符串时,它将在第一次单击时正确返回上一个字符和下一个字符,但从那时起,它将从anchorNode返回null或其他字符。好像它正在改变节点?