admin管理员组文章数量:1323730
I want to only allow selection from left to right, so the anchor node is always going to be the first node in the DOM tree (relative to the focus node).
Is there an easy way to test if the anchor node es before the focus node?
I want to only allow selection from left to right, so the anchor node is always going to be the first node in the DOM tree (relative to the focus node).
Is there an easy way to test if the anchor node es before the focus node?
Share Improve this question asked Nov 7, 2011 at 15:36 NullVoxPopuliNullVoxPopuli 65.2k76 gold badges214 silver badges361 bronze badges 1- 1 Not sure why this got a downvote. I think it's a decent question. – Tim Down Commented Nov 7, 2011 at 16:13
1 Answer
Reset to default 9Here's a simple way to do it that uses the fact that setting the end of a DOM Range to be at an earlier point in the document than the start of the range will collapse the range. I think this will break in Firefox 2, which had a bug in its handling of this, but the number of users of that browser is tiny.
function isSelectionBackwards() {
var backwards = false;
if (window.getSelection) {
var sel = window.getSelection();
if (!sel.isCollapsed) {
var range = document.createRange();
range.setStart(sel.anchorNode, sel.anchorOffset);
range.setEnd(sel.focusNode, sel.focusOffset);
backwards = range.collapsed;
range.detach();
}
}
return backwards;
}
本文标签:
版权声明:本文标题:javascript - window.getSelection(), how do you tell if the anchor node comes before the focus node? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742121237a2421710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论