插件来处理选择?

任何人都可以建议一个可以处理文本区域(跨浏览器)文本选择的插件吗?

我正在寻找一个插件,它可以:

  • 获取当前选择
  • 替换当前选择
  • 选择一个范围

(IE使用createTextRange而其他浏览器使用setSelectionRange 。应该存在一个可以执行上述操作的插件,这样我就不必重新发明轮子了。)

jQuery Fieldselection: https //github.com/localhost/jquery-fieldselection

请参阅: https : //github.com/localhost/jquery-fieldselection/blob/master/jquery-fieldselection.js

Implements: getSelection,replaceSelection

不实现: currentSelection

Rangy: Implements: getSelection,ReplaceSelection,CurrentSelection

(注意:不是jquery插件,但仍广泛适用)

跨浏览器的JavaScript范围和选择库。 它提供了一个简单的基于标准的API,用于在所有主流浏览器中执行常见的DOM范围和选择任务,从而在Internet Explorer和DOM兼容的浏览器之间抽象出这种function的完全不同的实现。

似乎实现了你想要的东西,对于demo来说: http : //rangy.googlecode.com/svn/trunk/demos/core.html

http://code.google.com/p/rangy/

另请参见: Jquery:非Textarea元素的selectionStart

Rangyinputs一个jquery插件,提供与Rangy类似的function,除了输入而不是任意DOM元素。

一个小型跨浏览器JavaScript库,用于获取和操作HTML元素中的选择。

演示: http : //rangyinputs.googlecode.com/svn/trunk/demos/textinputs_jquery.html

项目: http : //code.google.com/p/rangyinputs/

以下插件可以帮助您http://www.examplet.buss.hk/jquery/caret.php

这对我也有帮助

我尝试了很多这里的链接,还有jQuery插件库中的其他链接,但没有一个按照我想要的方式工作。

但是这里没有提到这个真的很有趣:

http://madapaja.github.io/jquery.selection/

希望它会有所帮助! 🙂

我创造了自己的。 只有2.6kb未压缩: http : //blog.gauffin.org/2012/02/a-javascript-selection-script/

 //jQuery is not required but supported. var selection = new textSelector($('#mytextarea')); selection.replace('New text'); // you can change selection: selection.select(1,10); // select char 1 to 10 // get selection console.log("Start char: " + selection.get().start); // check if anything is selected selection.isSelected(); // get the text var text = selection.text(); 

它可以在github上找到: https : //raw.github.com/jgauffin/griffin.editor/master/Source/textselector.js

http://codepen.io/mattsich/pen/MKvmxQ用于我构建的iOS样式选择。 结束就是这样:

 $(document).ready(function(){ $(".full-text").selectBars('.full-text', 'ipsum', function(){ $('.selected-text p').text($('.full-text').attr('data-selected')); }); });