JavaScript 的伟大思想,请帮忙。 我需要处理所选文本的复杂操作。
我有一些带有很多 html 标签的文本,我需要一个返回以下结果的函数:
用户选择具有该类的特定标记中的文本,例如 <div class="text">
,
并且该功能在以下情况下被激活:
$('.text').mouseup(function (e){...});
- 返回所选内容的开头
- 返回结尾
- 选择标识特定标签及其类别
此外:
- 要赋予 html 标签的字符数,
- 但你可以排除 一些标签示例,
好的,一些代码,我有:
$(function () {
$('.text').mouseup(function (e){
$(this).highlight(getSelectionText());
})
})
Here we highlight the desired part of the text, but did not return the correct position
In addition, the text is released coincidentally characters, and I need it for the position
最佳答案
您可能想尝试一下:
$(function () {
$('#detailBoxParagraph').mouseup(function (e){
var selectedText = getSelectionText();
$(this).removeHighlight();
$(this).highlight(selectedText);
var txt = $(this).text();
var pos = [];
var i = txt.indexOf(selectedText);
if(i > -1) {
pos.push({"start": i+1, "end": i + selectedText.length});
}
while (i != -1) {
i = txt.indexOf(selectedText, i + 1);
if(i > -1) {
pos.push({"start": i+1, "end": i + selectedText.length});
}
}
console.log(pos);
})
//alertSelection();
})
关于javascript - JS对选中文本的复杂操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31870984/