Javascript 高亮显示搜索到的关键字

  方法:利用文本替换,JS代码如下:

 1             // 高亮显示搜索到的关键字
 2              function  HeightLight(Keyword)
 3 ExpandedBlockStart.gifContractedBlock.gif             {
 4                //文本选择器
 5                var TextRange;
 6                //是否找到
 7                var Found=false;
 8                //找到的次数
 9                var Count = 0;
10                TextRange = document.body.createTextRange();
11               
12                Found = TextRange.findText(Keyword);
13                if (Found)
14ExpandedSubBlockStart.gifContractedSubBlock.gif                {
15                    Count++;
16                }

17
18                while (Found && Count > 0)
19ExpandedSubBlockStart.gifContractedSubBlock.gif                {
20
21                    TextRange.pasteHTML('<span style="background:yellow">' + Keyword + '</span>');
22                    //将滚动条定位到第一次查到的视口范围内
23                    if(Count==1)
24ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
25                        TextRange.scrollIntoView();
26                    }

27                    //继续查找
28                    Found = TextRange.findText(Keyword);
29                    if (!Found)
30ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
31                        Count = 0;
32                    }

33                    else
34ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
35                        Count++;
36                    }

37                }

38            }

 运行后效果如下:

 

 

全部代码:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >
< html >
    
< head >
        
< meta  http-equiv ="Content-Type"  content ="text/html; charset=UFT-8" >
        
< title > Untitled Document </ title >
ExpandedBlockStart.gifContractedBlock.gif        
< script >
            
//高亮显示搜索到的关键字
            function HeightLight(Keyword)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//文本选择器
                var TextRange;
                
//是否找到
                var Found=false;
                
//找到的次数
                var Count = 0;
                TextRange 
= document.body.createTextRange();
                
                Found 
= TextRange.findText(Keyword);
                
if (Found) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Count
++;
                }


                
while (Found && Count > 0
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{

                    TextRange.pasteHTML(
'<span style="background:yellow">' + Keyword + '</span>');
                    
//将滚动条定位到第一次查到的视口范围内
                    if(Count==1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        TextRange.scrollIntoView();
                    }

                    
//继续查找
                    Found = TextRange.findText(Keyword);
                    
if (!Found) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        Count 
= 0;
                    }

                    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        Count
++;
                    }

                }

            }

            


        
</ script >
    
</ head >
    
< body  onload ="HeightLight('window');" >
        
< h1 > page </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page </ h1 >         
        
< h1 > page </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page window 系统 </ h1 >
        
< h1 > page </ h1 >     
        
< h1 > page </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page </ h1 >
        
< h1 > page 系统 </ h1 >
        
< h1 > page window系统 </ h1 >
        
< h1 > page </ h1 >                             
    
</ body >
</ html >

转载于:https://www.cnblogs.com/qiantuwuliang/archive/2009/03/06/1404505.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好,关于您的问题,可以通过使用CSS样式来实现关键字高亮,并且可以使用JavaScript生成随机颜色。具体实现方法可以参考以下代码: ```html <style> .highlight { background-color: #FCD5CE; /* 设置关键字高亮颜色 */ } </style> <script> function highlightKeywords() { var keywords = ["JavaScript", "CSS", "HTML"]; // 关键字列表 var elements = document.getElementsByTagName("*"); // 获取所有元素 for (var i = ; i < elements.length; i++) { var element = elements[i]; if (element.nodeType === 3) { // 判断是否为文本节点 var text = element.nodeValue; var replacedText = text; for (var j = ; j < keywords.length; j++) { var keyword = keywords[j]; var regex = new RegExp(keyword, "gi"); // 创建正则表达式,忽略大小写 if (regex.test(text)) { var span = document.createElement("span"); span.className = "highlight"; span.style.backgroundColor = getRandomColor(); // 设置随机颜色 span.textContent = keyword; replacedText = replacedText.replace(regex, span.outerHTML); } } if (replacedText !== text) { var newNode = document.createElement("span"); newNode.innerHTML = replacedText; element.parentNode.replaceChild(newNode, element); } } } } function getRandomColor() { var letters = "0123456789ABCDEF"; var color = "#"; for (var i = ; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; } window.onload = highlightKeywords; // 页面加载完成后执行高亮函数 </script> ``` 以上代码可以实现在页面中将指定的关键字高亮,并且每个关键字高亮颜色都是随机生成的。希望对您有所帮助。 ### 回答2: 在JavaScript中,我们可以运用CSS样式和DOM操作来实现关键字高亮显示,并随机分配不同的颜色。 首先,我们需要为要高亮显示关键字添加一个class或者id。例如,我们可以将关键字用`span`标签包裹,并设置一个class名为"highlight",如下所示: ```html <p>这是一个<span class="highlight">关键字</span>的例子。</p> ``` 接下来,在JavaScript中,我们可以通过获取所有包含关键字的元素,并为它们随机分配不同的颜色样式。 ```javascript // 获取所有拥有highlight类名的元素 const highlightElements = document.getElementsByClassName('highlight'); // 生成随机颜色的函数 function getRandomColor() { const letters = '0123456789ABCDEF'; let color = '#'; for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; } // 为每个关键字元素添加随机颜色样式 for (let element of highlightElements) { const randomColor = getRandomColor(); element.style.backgroundColor = randomColor; element.style.color = 'white'; // 可选,为了确保文本可读性 } ``` 以上代码会为所有拥有highlight类名的元素随机分配不同的背景颜色。`getRandomColor`函数会生成一个随机的十六进制颜色码,然后在循环中为每个关键字元素设置`backgroundColor`样式为生成的颜色,可以添加`color`样式属性,以确保文本的可读性。 使用这种方式,关键字在页面中将以不同的随机颜色高亮显示。 ### 回答3: 要实现JavaScript关键字的随机不同颜色高亮,可以按照以下步骤进行: 1. 首先,创建一个包含所有关键字的数组。可以使用JavaScript中的关键字列表或自定义关键字列表。 2. 使用JavaScript的Math.random()函数生成一个0到1之间的随机数。 3. 将随机数乘以关键字颜色总数并向下取整,得到一个索引值。 4. 使用该索引值从颜色数组中获取对应的颜色。 5. 将获取到的颜色应用到关键字高亮样式中,例如使用CSS的color属性。 6. 重复步骤2至5,为每个关键字生成不同的随机颜色并应用到对应的高亮样式中。 以下是示例代码实现上述步骤: ```html <!DOCTYPE html> <html> <head> <style> .highlight { color: black; font-weight: bold; } </style> </head> <body> <p id="code"> var x = 5; for (var i = 0; i < 10; i++) { console.log(i); } var y = 10; </p> <script> // 关键字列表 var keywords = [ "var", "for", "console", "log", ]; // 颜色列表 var colors = ["red", "blue", "green", "yellow", "orange"]; var codeElement = document.getElementById("code"); // 遍历关键字列表 for (var i = 0; i < keywords.length; i++) { // 生成随机颜色索引 var randomIndex = Math.floor(Math.random() * colors.length); // 获取随机颜色 var randomColor = colors[randomIndex]; // 创建高亮样式 var highlightStyle = "color: " + randomColor; // 高亮关键字 var keywordRegExp = new RegExp("\\b" + keywords[i] + "\\b", "g"); codeElement.innerHTML = codeElement.innerHTML.replace( keywordRegExp, '<span class="highlight" style="' + highlightStyle + '">$&</span>' ); } </script> </body> </html> ``` 上述代码通过使用正则表达式将关键字找出并包裹在`<span>`标签中,该标签具有带有随机颜色类的高亮样式。每次运行代码,关键字都会被高亮为不同的随机颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值