简体   繁体   English

使用javascript突出显示用户选择的文本

[英]Highlight user selected text using javascript

I am working with someone else's code, and if I change too much, it breaks. 我正在使用别人的代码,如果我更改太多,它就会中断。 I need to highlight a portion of text that a user has highlighted. 我需要突出显示用户突出显示的文本部分。 It needs to be highlighted based on the ID of that element and not on the actual text. 需要根据该元素的ID而不是实际文本来突出显示它。 If it is based on the actual text, it will highlight every instance of that text and I only want the instance the user selected. 如果基于实际文本,它将突出显示该文本的每个实例,而我只希望用户选择该实例。 I have been beating my head for a week now trying to figure out how to edit this code so that it will do what I need it to do. 我已经动了一个星期的头,试图弄清楚如何编辑此代码,以便它可以执行我需要做的事情。 Can anyone help me figure out what I need to edit in order to get this accomplished? 谁能帮助我弄清楚我需要进行哪些编辑才能完成此工作?

//-------------------------------------------------------
// 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;
}
//-------------------------------------------------------

I'm not sure if it's exactly what you need, but it might be enough to get you started; 我不确定这是否正是您所需要的,但这可能足以让您入门。 I've managed to augment the code so that instead of a generic <font> tag, the search term is highlighted with a <span> tag with a class of .hilite . 我设法增加了代码,以使搜索词可以代替带有<span> .hilite <span>类的<span>标记,而不是通用的<font>标记。 The script then searches for the first occurrence of that span tag, and sets the parent's ID as a variable ( firstParent ). 然后,脚本搜索该span标记的首次出现,并将父代的ID设置为变量( firstParent )。

Updated example 更新的例子

The example uses jQuery, so you could refer to the parent element like this: 该示例使用jQuery,因此您可以像这样引用父元素:

$('#' + firstParent).before('<img src="myicon.png" />');

This would insert an icon before the containing parent. 这将在包含父项之前插入一个图标。

You can use this as a jumping-on point to augment the script to your needs. 您可以将其用作跳接点,以根据需要扩展脚本。 You should probably build in an extra layer of error-checking, in case the search term isn't found, or the parent element doesn't have an ID... 如果找不到搜索项或父元素没有ID,则可能应该在错误检查中添加额外的一层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM