getSelection不适用于在
input元素中选择的文本,但不适用于在页面中对元素进行的选择。
您可以使用
selectionStart和
selectionEnd这样的:
return document.activeElement.value.substring( document.activeElement.selectionStart, document.activeElement.selectionEnd)
您可能应该为此创建一个函数,而不是单行代码。也许您还想然后测试是否
document.activeElement确实是正确的元素类型,等等。当您使用它时,您甚至可以使其与IE9之前的浏览器兼容……([尽管很困难]
简单功能
这也将工作的
input或者
textarea没有重点控制:
function getInputSelection(el) { if (el.selectionStart !== undefined) { return el.value.substring(el.selectionStart, el.selectionEnd); }}// Example call:console.log(getInputSelection(document.activeElement));
广泛的jQuery插件
这提供了更多的跨浏览器兼容性(pre-
IE9),并且不仅支持以
jQuery插件的形式获取,而且还支持选择范围和文本的设置。它处理一个事实,即
CRLF字符序列以一种务实的方式算作一个字符位置(
LF仅在原处替换):
$.fn.selection = function (opt_bounds) { var bounds, inputRange, input, docRange, value; function removeCR(s) { // CRLF counts as one unit in text box, so replace with 1 char // for correct offsetting return s.replace(/rn/g, 'n'); } if (opt_bounds === undefined) { // Get if (!this.length) { return; } bounds = {}; input = this[0]; if (input.setSelectionRange) { // Modern browsers bounds.start = input.selectionStart; bounds.end = input.selectionEnd; } else { // Check browser support if (!document.selection || !document.selection.createRange) { return; } // IE8 or older docRange = document.selection.createRange(); // Selection must be confined to input only if (!docRange || docRange.parentElement() !== input) { return; } // Create another range that can only extend within the // input boundaries. inputRange = input.createTextRange(); inputRange.moveToBookmark(docRange.getBookmark()); // Measure how many characters we can go back within the input: bounds.start = -inputRange.moveStart('character', -input.value.length); bounds.end = -inputRange.moveEnd('character', -input.value.length); } // Add properties: bounds.length = bounds.end - bounds.start; bounds.text = removeCR(input.value). substr(bounds.start, bounds.length); return bounds; } // Set if (opt_bounds.text !== undefined) { opt_bounds.text = removeCR(opt_bounds.text); } return this.each(function () { bounds = $.extend($(this).selection(), opt_bounds); bounds.end = bounds.end === null ? this.value.length : bounds.end; if (opt_bounds.text !== undefined) { value = removeCR(this.value); this.value = value.substr(0, bounds.start) + bounds.text + value.substr(bounds.end); bounds.end = bounds.start + bounds.text.length; } if (this.setSelectionRange) { // Modern browsers // Call .focus() to align with IE8 behaviour. // You can leave that out if you don't care about that. this.focus(); this.setSelectionRange(bounds.start, bounds.end); } else if (this.createTextRange) { // IE8 and before inputRange = this.createTextRange(); inputRange.collapse(true); inputRange.moveEnd('character', bounds.end); inputRange.moveStart('character', bounds.start); // .select() will also focus the element: inputRange.select(); } });};
使用示例:
// Getconsole.log($('textarea').selection().text);// Set text$('textarea').selection({text: "Hello!"});// Set starting point of selection$('textarea').selection({start: 1});
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)