惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
L
Lohrmann on Cybersecurity
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
D
DataBreaches.Net
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
I
Intezer
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
InfoQ
宝玉的分享
宝玉的分享
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
博客园 - 司徒正美
H
Hacker News: Front Page
Y
Y Combinator Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
NISL@THU
NISL@THU
月光博客
月光博客
有赞技术团队
有赞技术团队
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
A
Arctic Wolf
博客园 - 【当耐特】
W
WeLiveSecurity
V
Visual Studio Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog

博客园 - jeky

rails 路由配置时 URL 地址如何匹配下划线? github.com 不能访问怎么办? Linux - 显示日历的命令 cal 用法 Linux - echo 命令如何追加文本? 如何解决ssh连接后长时间不操作断线的问题? Rails 中如何使用全局变量? raw 允许输出html字符 Linux 用户及组 话说调试 全等(===) 委托的协变与逆变 判断一个整数是奇数还是偶数 - jeky - 博客园 选择排序法 让我笔试吃亏"单例模式" Javascript中正则表达式的相识知识 对 TextBox 设置css属性 注意void()里面不能空 - jeky - 博客园 OWC11 实例 访问数组元素的 3 种方法 Internet Explorer 7.0(IE7.0)简体中文版
用Javascript实现关键词的高亮显示
jeky · 2009-03-13 · via 博客园 - jeky

  最近在搞一个网站,需要在内容区域高亮显示一些关键词。本来想在后台页面用c#实现,后来感觉这样做不太可取。因为我想高亮的高键词有n个之多,在服务端循环处理的话,势必会影响效率、占用服务器资源。所以,才考虑使用JS来实现,代码如下:

 一个小类(可以放置到一个*.js文件中)

    function XP_Highlight()
    
{
        
this.KeyWords = null;
        
        
// 格式化关键词
        this.formatKeyword = function(content, keyword)
        
{
            keyword 
= keyword.replace(/(^\s*)|(\s*$)/g, "");
            
if(keyword == '')
                
return content;
            
var reg = new RegExp('('+keyword+')''gi');
            
return content.replace(reg, '<em>$1</em>');
        }

        
        
// 重绘内容区域
        this.refreshContent = function(contentID)
        
{
            
var content = document.getElementById(contentID).innerHTML;
            
for(var i = 0; i < keywords.length; i ++)
            
{
                
var strKey = keywords[i].toString();
                
var arrKey = strKey.split(',');
                
for(var j = 0; j < arrKey.length; j ++)
                
{
                    
var key = arrKey[j];
                    content 
= this.formatKeyword(content, key);
                }

            }

            document.getElementById(contentID).innerHTML 
= content;
        }

    }

页面调用:

    // 关键词定义
    var keywords = 
    [
        [
'心情好转,好梦一场,,真不错,真好吃,哈哈,嘻嘻'],
        [
'头晕脑胀,疲乏气短,索然无味,,,折腾,生病,抱怨,疾病,累,疼,病'],
        [
'怎么了,啊,呀,喂']
    ];
    
    
    $(document).ready(
function(){
        
var hl = new XP_Highlight();
        hl.keywords 
= keywords; // 这里是关键词的定义
        hl.refreshContent(
'res'); // 这里是要格式化内容的元素Id号
    }
); 

CSS定义(可以设置多种风格,以支持不同类型的关键词):

em { font-size:small; color:#CC0033; font-style:normal; }

结束语:

以上代码在 FF3.0 及 IE8兼容模式下测试通过。
如何各位有其他方法或建议的话,请在这里回复,先谢谢了!