window.getSelection()忽略某些标记

时间:2014-10-01 07:34:57

标签: javascript

以下是我的HTML代码:

<div id="text">This is <span class="ignore">some</span> short text.</div>

当我使用鼠标并从“是”选择“短”时,通过使用在线找到的以下功能,

function getSelectionCharOffsetsWithin(element) {
    var start = 0, end = 0;
    var sel, range, priorRange;
    if (typeof window.getSelection != "undefined") {
        range = window.getSelection().getRangeAt(0);
        priorRange = range.cloneRange();
        priorRange.selectNodeContents(element);
        priorRange.setEnd(range.startContainer, range.startOffset);
        start = priorRange.toString().length;
        end = start + range.toString().length;
    } else if (typeof document.selection != "undefined" &&
            (sel = document.selection).type != "Control") {
        range = sel.createRange();
        priorRange = document.body.createTextRange();
        priorRange.moveToElementText(element);
        priorRange.setEndPoint("EndToStart", range);
        start = priorRange.text.length;
        end = start + range.text.length;
    }
    return {
        start: start,
        end: end
    };
}

我将获得开始范围= 5,结束范围= 20。 但我希望范围计算忽略<span class="ignore">,这样我才能得到起始范围= 5和结束范围= 14.

我在处理之前将文本复制到另一个虚拟div,但似乎window.getSelection()只会得到原始文件,是否有专家知道如何忽略<span class="ignore">内的文本所以我可以得到准确的范围?

3 个答案:

答案 0 :(得分:0)

JSFIDDLE

<div class="content">
Vivamus <span>luctus</span> urna sed urna ultricies ac tempor dui sagittis.
</div>

$(".content").on("mouseup", function () {
    var start = window.getSelection().baseOffset;
    var end = window.getSelection().focusOffset;
    if (start < end) {
        var start = window.getSelection().baseOffset;
        var end = window.getSelection().focusOffset;
    } else {
        var start = window.getSelection().focusOffset;
        var end = window.getSelection().baseOffset;
    }
   alert(window.getSelection());
    alert(start + ", " + end);
});

答案 1 :(得分:0)

我设计了一个解决方案, 请检查 - http://jsfiddle.net/ghufranne/awc6c9rp/1/

  function calcTextLength(){
    alert("d");
    var elem = document.getElementsByClassName('p1')[0];
    elem.text='sss';
    alert(elem);
var tex = getSelectedTextWithin(elem);
    elem = document.getElementsByClassName('ignore')[0];

   ignoreText = getSelectedTextWithin(elem);
       alert(tex.length-ignoreText.length);



}


function getSelectedTextWithin(el) {
    var selectedText = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection(), rangeCount;
        if ( (rangeCount = sel.rangeCount) > 0 ) {
            var range = document.createRange();
            for (var i = 0, selRange; i < rangeCount; ++i) {
                range.selectNodeContents(el);
                selRange = sel.getRangeAt(i);
                if (selRange.compareBoundaryPoints(range.START_TO_END, range) == 1 && selRange.compareBoundaryPoints(range.END_TO_START, range) == -1) {
                    if (selRange.compareBoundaryPoints(range.START_TO_START, range) == 1) {
                        range.setStart(selRange.startContainer, selRange.startOffset);
                    }
                    if (selRange.compareBoundaryPoints(range.END_TO_END, range) == -1) {
                        range.setEnd(selRange.endContainer, selRange.endOffset);
                    }
                    selectedText += range.toString();
                }
            }
        }
    } else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
        var selTextRange = document.selection.createRange();
        var textRange = selTextRange.duplicate();
        textRange.moveToElementText(el);
        if (selTextRange.compareEndPoints("EndToStart", textRange) == 1 && selTextRange.compareEndPoints("StartToEnd", textRange) == -1) {
            if (selTextRange.compareEndPoints("StartToStart", textRange) == 1) {
                textRange.setEndPoint("StartToStart", selTextRange);
            }
            if (selTextRange.compareEndPoints("EndToEnd", textRange) == -1) {
                textRange.setEndPoint("EndToEnd", selTextRange);
            }
            selectedText = textRange.text.length;
        }
    }

    return selectedText;
}

答案 2 :(得分:0)

如果您只需要文本(就像我一样),那么我的简单解决方案是在获取文本之前隐藏内部div,然后显示它。以下是如何获得开始和结束的片段。可能是一个更好的方法,但为我工作。

&#13;
&#13;
function getSelectionCharOffsetsWithin(element) {
  var start = 0,
    end = 0;
  var sel, range, priorRange, text;
  $('.ignore').hide();
  if (typeof window.getSelection != "undefined") {
    sel = window.getSelection();
    text = sel + '';
    range = window.getSelection().getRangeAt(0);
    priorRange = range.cloneRange();
    priorRange.selectNodeContents(element);
    priorRange.setEnd(range.startContainer, range.startOffset);
    start = priorRange.toString().length;
    end = start + (sel + '').length;
  } else if (typeof document.selection != "undefined" &&
    (sel = document.selection).type != "Control") {
    text = sel + '';
    range = sel.createRange();
    priorRange = document.body.createTextRange();
    priorRange.moveToElementText(element);
    priorRange.setEndPoint("EndToStart", range);
    start = priorRange.text.length;
    end = start + (sel + '').length;
  }
  $('.ignore').show();
  return {
    start: start,
    end: end,
    text: text
  };
}

function run() {
  var v = getSelectionCharOffsetsWithin(document.getElementById("text"));
  document.getElementById("res").innerHTML = "start: " + v.start + " end: " + v.end + " text: " + v.text;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div id="text">This is <span class="ignore">some</span> short text.</div>
Select a piece of text above, then click <button onclick="run();">Run</button>
<div id="res"></div>
&#13;
&#13;
&#13;

编辑:jquery选择器拼写错误。