【发布时间】:2015-10-30 11:01:34
【问题描述】:
我想在文本框中找到所选文本之前和之后的文本
例如
<input id="selectionBox" />
示例: selectionBox 输入值等于“检查输入地址...”
用户在“检查添加”之后和“用于键入...”之前选择了“r”,我想将“r”之前和之后存储在单独的变量中
$('#selectionBox').select(function(e) {
var start = e.target.selectionStart;
var end = e.target.selectionEnd;
var selectedtext = $('#selectionBox').val().substring(start, end);
// find before and after selected text
//var beforeSel = before selection;
//var afterSel = after selection;
})
编辑 1: 我想在文本之前和之后找到像这样更改 selctionBox 值的文本
var textboxValue = beforeSel + "<blank>" + selectedtext + "<blank>" + afterSel;
$("#selectionBox").val(textboxValue);
怎么办?谢谢
【问题讨论】:
-
如果你的相等字符很少,它应该如何工作?如果用户选择“e”?
-
我编辑了我的问题,请查看,谢谢
-
先获取#('selectionBox').val()的长度。那么你可以使用 substring() 方法来获取所有三个值