我正在使用其他人的代码,如果我更改太多,它就会中断。我需要突出显示用户突出显示的部分文本。它需要根据该元素的 ID 而不是实际文本突出显示。如果它基于实际文本,它将突出显示该文本的每个实例,我只想要用户选择的实例。一个星期以来,我一直在绞尽脑汁,试图弄清楚如何编辑这段代码,以便它能够完成我需要它做的事情。谁能帮我弄清楚我需要编辑什么才能完成这项工作?
//-------------------------------------------------------
// Name: doHighlight()
// Find all occurences of the search term in the given text,
// and add some "highlight" tags to them (we're not using a
// regular expression search, because we want to filter out
// matches that occur within HTML tags and script blocks, so
// we have to do a little extra validation)
//-------------------------------------------------------
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
{
var newText = "";
var i = -1;
var lcSearchTerm = searchTerm.toLowerCase();
var lcBodyText = bodyText.toLowerCase();
var temp = 0;
var counter = 0;
//********************************************************************
//********************************************************************
//********************************************************************
//Here is the ID of the <p> element where our text was selected
alert(textId);
//Here is the text of the ID of the <p> element we selected
alert(document.getElementById(textId).innerHTML);
//********************************************************************
//********************************************************************
//********************************************************************
while (bodyText.length > 0)
{
i = lcBodyText.indexOf(lcSearchTerm, i++);
if (i < 0)
{
newText += bodyText;
bodyText = "";
}
else
{
// skip anything inside an HTML tag
if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i))
{
// skip anything inside a <script> block
if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i))
{
//********************************************************************
//********************************************************************
//********************************************************************
// Writes the document til it reaches the search term, then adds highlightStartTag then the search term then highlightEndTag
// Then writes more of the document until the search term is reached again
// Needs to search for the term based on the id="" given to the <p> element
// Then add the highlightStartTag and highlightEndTag
newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
bodyText = bodyText.substr(i + searchTerm.length);
lcBodyText = bodyText.toLowerCase();
i = +1;
//********************************************************************
//********************************************************************
//********************************************************************
}
}
}
//break;
}
//newText += bodyText;
return newText;
}
//-------------------------------------------------------