相关疑难解决方法(0)

使用getSelection选择整个单词

我想使用该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 …
Run Code Online (Sandbox Code Playgroud)

javascript

10
推荐指数
2
解决办法
8214
查看次数

标签 统计

javascript ×1