我想使用该getSelection
函数从文章中选择单词到视图框.
这是我的代码:http://jsfiddle.net/xQKNh/2/.
现在我想问一下,如何使用JavaScript来选择整个单词?
为了解释,
Is your question about programming?
Run Code Online (Sandbox Code Playgroud)
在我的代码中,如果我选择r question about pro
,view box
将显示
r question about pro
Run Code Online (Sandbox Code Playgroud)
但是如何完成这些单词呢?所以输出:
your question about programming.
Run Code Online (Sandbox Code Playgroud)
Javascript代码:
function getSelected() {
if(window.getSelection) { return window.getSelection(); }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
}
$(document).ready(function() {
$('#content-area').mouseup(function() {
var selection = getSelected();
if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
$('#show-text').html(selection)
}
});
});
Run Code Online (Sandbox Code Playgroud)
Tim*_*own 21
最近版本的Firefox和WebKit浏览器具有对象的内置modify()
(参见工作进展规范)方法Selection
.从版本4开始,IE有一种完全不同的方式.在任何一种情况下,这种方法都具有跨元素边界工作的显着优势.
以下示例适用于IE 4 +,Firefox 4+以及Safari和Chrome在过去几年中发布的版本.Opera尚未支持对象的modify()
方法Selection
.
2011年10月20日更新
我已经重写了这个(大部分)现在正在工作(它在之前的非IE浏览器中无法正常工作,正如评论中所指出的那样).不幸的是,选择扩展在非IE中过于贪婪,并且如果已经选择了整个单词,则将选择扩展到下一个单词,但我现在看不到一个简单的方法.
更新2012年6月11日
我现在更新了这个问题,从这个问题的答案中得到了改进.感谢Matt M和Fong-Wan Chau.
现场演示:http://jsfiddle.net/rrvw4/23/
码:
function snapSelectionToWord() {
var sel;
// Check for existence of window.getSelection() and that it has a
// modify() method. IE 9 has both selection APIs but no modify() method.
if (window.getSelection && (sel = window.getSelection()).modify) {
sel = window.getSelection();
if (!sel.isCollapsed) {
// Detect if selection is backwards
var range = document.createRange();
range.setStart(sel.anchorNode, sel.anchorOffset);
range.setEnd(sel.focusNode, sel.focusOffset);
var backwards = range.collapsed;
range.detach();
// modify() works on the focus of the selection
var endNode = sel.focusNode, endOffset = sel.focusOffset;
sel.collapse(sel.anchorNode, sel.anchorOffset);
var direction = [];
if (backwards) {
direction = ['backward', 'forward'];
} else {
direction = ['forward', 'backward'];
}
sel.modify("move", direction[0], "character");
sel.modify("move", direction[1], "word");
sel.extend(endNode, endOffset);
sel.modify("extend", direction[1], "character");
sel.modify("extend", direction[0], "word");
}
} else if ( (sel = document.selection) && sel.type != "Control") {
var textRange = sel.createRange();
if (textRange.text) {
textRange.expand("word");
// Move the end back to not include the word's trailing space(s),
// if necessary
while (/\s$/.test(textRange.text)) {
textRange.moveEnd("character", -1);
}
textRange.select();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里的技巧是使用DOM 范围.然后,您可以向任一方向扩展范围,直到您到达某个空间.所以(警告,未经过广泛测试):
function getSelected() {
if(window.getSelection) { return window.getSelection(); }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
}
function expand(range) {
if (range.collapsed) {
return;
}
while (range.toString()[0].match(/\w/)) {
range.setStart(range.startContainer, range.startOffset - 1);
}
while (range.toString()[range.toString().length - 1].match(/\w/)) {
range.setEnd(range.endContainer, range.endOffset + 1);
}
}
$(document).ready(function() {
$('#content-area').mouseup(function() {
var selectionRange = getSelected().getRangeAt(0);
var start = selectionRange.startOffset;
expand(selectionRange);
var selection = selectionRange.toString();
if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
$('#show-text').html(selection)
}
});
});
Run Code Online (Sandbox Code Playgroud)
优化留给读者练习.
归档时间: |
|
查看次数: |
8214 次 |
最近记录: |