正在获取当前选定的文本
Getting currently selected text
我试图使用window.getSelection()
获取输入中当前选定的文本,但总是得到一个空字符串:
expect(browser.executeScript("return window.getSelection().toString();")).toEqual("test");
结果进入:
Expected '' to equal 'test'.
使用angularjs.org作为目标站点的完整可重复测试:
describe("My test", function () {
beforeEach(function () {
browser.get("https://angularjs.org/");
});
it("should select text in an input", function () {
var query = element(by.css("input.search-query"));
query.sendKeys("test");
query.sendKeys(protractor.Key.chord(protractor.Key.COMMAND, "a"));
expect(browser.executeScript("return window.getSelection().toString();")).toEqual("test");
});
});
请注意,我实际上看到输入的文本是用COMMAND+"a"选择的。
我做错了什么?
使用量角器2.5.1,firefox 41。
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插件
这提供了更多的跨浏览器兼容性(IE9
之前),并且以jQuery
插件的形式不仅支持获取,还支持设置选择范围和文本。它以一种实用的方式处理了CRLF
字符序列算作一个字符位置的事实(仅用LF
代替):
/**
* jQuery plug-in for getting/setting the selection range and text
* within input/textarea element(s). When the selection is set,
* the element will receive focus. When getting the selection,
* some browsers require the element to have focus (IE8 and below).
* It is up to the caller to set the focus first, if so needed.
* @this {jQuery} Input/textarea element(s).
* @param {object} opt_bounds When provided, it sets the range as follows:
* @param {number} opt_bounds.start Optional start of the range. If not
* provided, the start point of the range is not altered.
* @param {number} opt_bounds.end Optional end of the range. If not
* provided, the end point of the range is not altered. If null, the end
* of the text value is assumed.
* @param {number} opt_bounds.text Optional text to put in the range. If
* not provided, no change will be made to the range's text.
* @return {jQuery|object|undefined} When setting: the same as @this to
* allow chaining, when getting, an object {start, end, text, length}
* representing the selection in the first element if that info
* is available, undefined otherwise.
*/
$.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(/'r'n/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();
}
});
};
示例用法:
// Get
console.log($('textarea').selection().text);
// Set text
$('textarea').selection({text: "Hello!"});
// Set starting point of selection
$('textarea').selection({start: 1});
相关文章:
- 使用谷歌应用程序脚本从工作表中获取值并将其显示在文本框中
- 如何获取经过编辑的文本
- 使用 jQuery 从下拉列表中获取所选文本
- 如何获取文本框组的值,并使用jquery将它们放入(key:Value)数组中
- 使用格式化文本获取 DIV 或跨度的动态宽度高度
- 通过搜索 td 文本获取嵌套表 ID
- 将 HTML 元素的文本获取到数组中
- 如何从响应文本获取 JSON 字符串到 JavaScript 中
- 如何根据选项文本获取选项索引
- 通过其文本获取dom节点
- 如何通过锚文本获取对象
- 在Javascript/JQuery中从超链接文本获取Href
- 如何将函数中的文本获取到此通知警报中
- 根据指定的文本获取标记
- 通过匹配文本获取json数据到数组中
- 如何通过id和输出文本获取选项元素
- 通过相邻的TH内部文本获取TD中的文本
- 如何将任何指定网页中的文本获取到变量中
- 通过内部文本获取所有元素
- 从给定的HTML文本获取JQuery