admin管理员组文章数量:1221386
there are lots of code to get selection in a page, but i want a code to get selection inside a div, if the selection is outside of my div, the function must return empty string;
there is a jquery plugin that works only for textarea but not div. (here)
thanks
there are lots of code to get selection in a page, but i want a code to get selection inside a div, if the selection is outside of my div, the function must return empty string;
there is a jquery plugin that works only for textarea but not div. (here)
thanks
Share Improve this question asked Apr 27, 2011 at 8:35 ehsanehsan 7873 gold badges13 silver badges28 bronze badges3 Answers
Reset to default 13This is slightly verbose because of long-winded boundary comparisons and because IE takes a different approach from other browsers, but does the job in all major browsers. It also handles multiple selections in Firefox.
jsFiddle: http://jsfiddle.net/Q982A/2/
Code:
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;
}
}
return selectedText;
}
Alternatively, you could use my Rangy library, and the code becomes:
function getSelectedTextWithin(el) {
var selectedText = "";
var sel = rangy.getSelection(), rangeCount = sel.rangeCount;
var range = rangy.createRange();
range.selectNodeContents(el);
for (var i = 0; i < rangeCount; ++i) {
selectedText += sel.getRangeAt(i).intersection(range);
}
return selectedText;
}
You can use:
var node = window.getSelection ?
window.getSelection().focusNode.parentNode:
document.selection.createRange().parentElement();
to get the element in which the selection end.
Then you can determine if that element is inside your div.
You can achieve this by using the Text select plugin - http://plugins.jquery.com/node/7411
In the binding code you can then interrogate the event target to see if it is within the element you require.
<script type="text/javascript">
$(function() {
$(document).bind('textselect', function(e) {
if ($(e.target).attr("id") == "myPara") {
alert(e.text);
}
});
});
</script>
<p id="myPara">Lorem ipsum dolor sit amet consectetuer Lorem sapien hendrerit elit Cum. Morbi Curabitur convallis sagittis vitae sem parturient augue orci tortor eget. Et porttitor eros id consectetuer est molestie tellus fames netus nec. Ullamcorper id Nam eros sed nascetur Maecenas turpis at laoreet eleifend. Lorem id parturient dapibus Aenean cursus congue justo auctor pharetra orci. Hac amet.</p>
<p id="noSelect">Lorem ipsum dolor sit amet consectetuer Lorem sapien hendrerit elit Cum. Morbi Curabitur convallis sagittis vitae sem parturient augue orci tortor eget. Et porttitor eros id consectetuer est molestie tellus fames netus nec. Ullamcorper id Nam eros sed nascetur Maecenas turpis at laoreet eleifend. Lorem id parturient dapibus Aenean cursus congue justo auctor pharetra orci. Hac amet.</p>
本文标签: how to get selection inside a div using jqueryjavascriptStack Overflow
版权声明:本文标题:how to get selection inside a div using jqueryjavascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739311050a2157582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论