admin管理员组文章数量:1415484
I use this javascript code to get the currently highlighted selection.
var selection = window.getSelection()
If the highlight is a section of text all within a <div>
, how can I get the offset from the beginning of the <div>
and the length of the highlight? (the length is not just the length of the text, it should be the actual length of the html code for that text)
I use this javascript code to get the currently highlighted selection.
var selection = window.getSelection()
If the highlight is a section of text all within a <div>
, how can I get the offset from the beginning of the <div>
and the length of the highlight? (the length is not just the length of the text, it should be the actual length of the html code for that text)
- Offset from the beginning of what? – Tim Down Commented Oct 11, 2010 at 11:11
1 Answer
Reset to default 5You can get the length of the selected HTML as follows:
function getSelectionHtml() {
var sel, html = "";
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
var frag = sel.getRangeAt(0).cloneContents();
var el = document.createElement("div");
el.appendChild(frag);
html = el.innerHTML;
}
} else if (document.selection && document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
return html;
}
alert(getSelectionHtml().length);
本文标签: HTML document selection using javascriptStack Overflow
版权声明:本文标题:HTML document selection using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745234444a2648970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论