I am using jQuery to highlight some similar parts of the words in HTML page.
To highlight the similar words, I tried to change html content. So at first, I get html content of the element. And change that html by adding span tag to highlight similar words in specific color.
I could make the html content successfully and I replace the html content by using jQuery.
But the problem is here. Some parts of the text are automatically highlighted when changing html content.
When I comment that part, it does not happen. So I think the problem is here.
Here is the screenshot of the problem
For example, when I select 42, from first part of html to the 42 are automatically highlighted.
Here is my html file.
https://drive.google.com/file/d/1lWlZW1psmYLIr7g-nQahWzyPVoN1q-hT/view?usp=sharing
Appreciate any help. Thanks
var selection = '';
var selectedObj = null;
var originHTML = $('#mainContent').html();
$('#mainContent').mousemove(function(event) {
getSelectedText();
if (selectedObj.focusNode) {
var wholeText = selectedObj.focusNode.nodeValue;
if (wholeText != null ) {
var startOffset = (selectedObj.baseOffset > selectedObj.focusOffset ? selectedObj.focusOffset : selectedObj.baseOffset);
var endOffset = (selectedObj.baseOffset > selectedObj.focusOffset ? selectedObj.baseOffset : selectedObj.focusOffset);
var selectedText = wholeText.slice(startOffset, endOffset);
if (selectedText != '' && selectedText != ' ' && selectedText != '.' && selectedText != ',' && selectedText != '(' && selectedText != ')' && selectedText != '+' && selectedText != '=' && selectedText != '/' ) {
if (selection != selectedText) {
selection = selectedText;
if (wholeText != null ) {
if (
(wholeText[startOffset - 1] >= '0' && wholeText[startOffset - 1] <= '9') ||
(wholeText[startOffset - 1] >= 'a' && wholeText[startOffset - 1] <= 'z') ||
(wholeText[startOffset - 1] >= 'A' && wholeText[startOffset - 1] <= 'Z') ||
(wholeText[endOffset] >= '0' && wholeText[endOffset] <= '9') ||
(wholeText[endOffset] >= 'a' && wholeText[endOffset] <= 'z') ||
(wholeText[endOffset] >= 'A' && wholeText[endOffset] <= 'Z')
) {
} else {
var wholeHtml = originHTML;
var newTag = '<span class="selectedCom">' + selectedText + '</span>';
var inCompleteNewTag = '<span class="selectedIncom">' + selectedText + '</span>';
var indicies = [];
var indic = -1;
while (1) {
indic = wholeHtml.indexOf(selectedText, indic + 1);
if (indic == -1) {
break;
}
indicies.push(indic);
}
var searchIndex = 0;
for (searchIndex = indicies.length - 1; searchIndex >= 0; searchIndex --) {
var strIndex = indicies[searchIndex];
if (
(wholeHtml[strIndex - 1] >= '0' && wholeHtml[strIndex - 1] <= '9') ||
(wholeHtml[strIndex - 1] >= 'a' && wholeHtml[strIndex - 1] <= 'z') ||
(wholeHtml[strIndex - 1] >= 'A' && wholeHtml[strIndex - 1] <= 'Z') ||
(wholeHtml[strIndex + selectedText.length] >= '0' && wholeHtml[strIndex + selectedText.length] <= '9') ||
(wholeHtml[strIndex + selectedText.length] >= 'a' && wholeHtml[strIndex + selectedText.length] <= 'z') ||
(wholeHtml[strIndex + selectedText.length] >= 'A' && wholeHtml[strIndex + selectedText.length] <= 'Z')
) {
} else {
while (1) {
if (wholeHtml[strIndex] == '<' || wholeHtml[strIndex] == '>') {
break;
}
strIndex --;
}
if (wholeHtml[strIndex] == '>') {
var prevString = wholeHtml.slice(0, indicies[searchIndex] );
var lastString = wholeHtml.slice(indicies[searchIndex] + selectedText.length, wholeHtml.length);
wholeHtml = prevString + (indicies.length == 1 ? inCompleteNewTag : newTag) + lastString;
}
}
}
$('#mainContent').html(wholeHtml);
}
}
}
}
}
}
});
$('#mainContent').mousedown(function() {
if (selection == undefined || selection == null || selection != '') {
selection = '';
$('#mainContent').html(originHTML);
}
});
function getSelectedText() {
if (window.getSelection) {
selectedObj = window.getSelection();
return window.getSelection().toString();
}
else if (document.selection) {
return document.selection.createRange().text;
}
return '';
}
question from:
https://stackoverflow.com/questions/65643741/some-parts-of-the-html-page-are-automatically-highlighted-after-changing-html 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…