gpt4 book ai didi

javascript - JS对选中文本的复杂操作

转载 作者:行者123 更新时间:2023-11-28 00:00:13 25 4
gpt4 key购买 nike

JavaScript 的伟大思想,请帮忙。我需要处理所选文本的复杂操作。

我有一些带有很多 html 标签的文本,我需要一个返回以下结果的函数:

用户选择具有该类的特定标记中的文本,例如 <div class="text"> ,

并且该功能在以下情况下被激活:

$('.text').mouseup(function (e){...});
  • 返回所选内容的开头
  • 返回结尾
  • 选择标识特定标签及其类别

此外:

  • 要赋予 html 标签的字符数,
  • 但你可以排除一些标签示例,

好的,一些代码,我有:

$(function () {
$('.text').mouseup(function (e){
$(this).highlight(getSelectionText());
})
})

this demo

Here we highlight the desired part of the text, but did not return thecorrect position

In addition, the text is released coincidentallycharacters, 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com