admin管理员组文章数量:1245641
How can I select a part of the text and process by jQuery? For example, I have a text as
<div id="test">This is an example text here</div>
I select few words (not the whole div) with mouse, and want to show these words (part of #test
) in
<div id="target"></div>
How can I select a part of the text and process by jQuery? For example, I have a text as
<div id="test">This is an example text here</div>
I select few words (not the whole div) with mouse, and want to show these words (part of #test
) in
<div id="target"></div>
Share
Improve this question
edited Jan 3, 2012 at 12:43
Curtis
103k68 gold badges277 silver badges357 bronze badges
asked Jan 3, 2012 at 12:29
GooglebotGooglebot
15.7k45 gold badges144 silver badges247 bronze badges
0
6 Answers
Reset to default 6If you mean by select text then show it into an element.
var selectedText;
if(window.getSelection)
{
selectedText = window.getSelection();
}
else if(document.getSelection)
{
selectedText = document.getSelection();
}
else if(document.selection)
{
selectedText = document.selection.createRange();
}
$('#target').text(selectedText.toString());
Or by typical showing an element text into another element, then you use:
$('#target').text($('#test').text());
You can use the text method like so:
(function(namespace) {
namespace.getSelected = function() {
var t = '';
if (window.getSelection) {
t = window.getSelection();
} else if (document.getSelection) {
t = document.getSelection();
} else if (document.selection) {
t = document.selection.createRange().text;
}
return t;
};
}(Namespace || {}))
$(function() {
$('#test').on("mouseup", function() {
var selectedText = Namespace.getSelected();
if (selectedText) {
$('#target').text(selectedText);
}
});
});
Here's how. The following works in all major browsers, including IE 6.
function getSelectedText() {
var selectedText = "", sel;
if (window.getSelection) {
selectedText = "" + window.getSelection();
} else if ( (sel = document.selection) && sel.type == "Text") {
selectedText = sel.createRange().text;
}
return selectedText;
}
$('#target').text( getSelectedText() );
Please refer
http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html
$.fn.selectRange = function(start, end) {
return this.each(function() {
if(this.setSelectionRange) {
this.focus();
this.setSelectionRange(start, end);
} else if(this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
});
};
Reference: http://programanddesign./js/jquery-select-text-range/
You would have to use http://www.examplet.buss.hk/jquery/caret.php plugin and you could easily get the selected text
// Get start pos in intput box with id="box1" $("#box1").caret().start
// Get end pos $("#box1").caret().end
// Get selected text $("#box1").caret().text
本文标签: javascriptHow to select text with jQueryStack Overflow
版权声明:本文标题:javascript - How to select text with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740141144a2230915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论