高亮显示搜索到的关键字-jquery.highlight.js的用法!

闲话少说,直接看需求: 在一篇文章中查找关键字,找到后高亮显示.

这种需求可以用js实现,可以用jquery实现,殊不知jquery有现成的插件,亲,你不用自己实现了,只需要引入jquery-hightlight.js

注意:如下body内容如此复杂只是为了测试,没有其他目的.

<!DOCTYPE html>
<html>
  <head>
    <title>regex.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  	<script type="text/javascript" src="../jquery/jquery-1.11/jquery.min.js"></script>
  	<script type="text/javascript" src="../jquery/jquery-highlight/jquery-hightlight.js"></script>
  	<style>
  		.highlight {background-color: #FFFF88; }
  	</style>
  	<script>
  		$(function(){
  			$('#search').click(function(){
  				var key = $('#key').val();
  	  			if(key.length > 0) {
  	  				var body = $('#body');
  	  				body.removeHighlight();
  	  				body.highlight(key);
  	  			} else {
  	  				alert("请输入关键字!")
  	  			}
  			});
  		});
  	</script>
  </head>
  <body>
  	<div>
  		<input id="key" type="text">
  		<input id="search" type="button" value="搜索">
  	</div>
    <div id="head">
    	This is a test head!
    	<div>
    		input the search key.
    	</div>
    	<div>
    		click "搜索" button.
    	</div>
    </div>
    <h1>如下是搜索区域!</h1>
    <div id="body">
    	This is a test body!
    	<div>
    		This is a test, a test, test, tes, te, t!
    		Do you know and listen this test, I think you don't know this test!
    	</div>
    	<div>
    		这是一个简答的测试,测试.
    		<p>你知道或者听过这个测试吗,我认为你没有听过这个测试!</p>
    	</div>
    	<div>
    		This is a test, a test, test, tes, te, t!
    		I know't know this test, yes, this test is a not famous test!
    		<p>Follow me test, to test the test! I don't know what do you say?</p>
    	</div>
    	<div>
    		<div>
	    		这是一个简答的测试,测试.
	    		我不知道这个测试,是的,这不是一个注明的测试,
	    		<span>跟随我,去测试这个测试,我都不知道我在说什么!</span>
    		</div>
    	</div>
    	<div>
    		What do you say? test, only a test?
    		<p>你们说什么呢?测试,一个测试?</p>
    	</div>
    </div>
  </body>
</html>

实现效果如何呢?看如下效果图:

怎么样,够简单吧!当然他的功能不仅如此,快点网上去搜索看吧!如下是我参考的资料:

http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html

使用方法如下:

  • 在<head>中引入jquery文件及插件文件jquery.highlight.js
    <script type="text/javascript" src="../js/jquery-1.4.2.js"></script><script type="text/javascript" src="../js/jquery.highlight.js"></script><script type="text/javascript">function hightlight(str){$(".highlight").highlight(str);}</script>
  • 在页面Body部分加入从表单传递过来的关键字<body onload="hightlight(‘<%=keywords%>‘);">
  • Css样式:<style type="text/css">        .highlight{background-color: #FFAEB1;}        </style>
  • 具体介绍如下:
  • 在css中或通过jquery指定高亮区域的颜色:
     .highlight {background-color: #FFFF88; }
    $(".highlight").css({ backgroundColor: "#FFFF88" });
  • 设置需要高亮显示的词语或通过事件触发高亮显示。例如:
          $("h1").highlight("highlight");      //高亮显示所有h1标签中的“highligh”
          $("body p").unhighlight();      //取消所有body中段落里的高亮显示;
          $("p").highlight("jQuery highlight plugin");      //高亮段落中的词条 “jQuery highlight plugin”
          $("p").highlight(["jQuery", "highlight", "plugin"]);      //高亮段落中的 “jQuery”,“highlight”及“plugin”
          $("p").highlight("Highlight", { caseSensitive: true });      //高亮段落中的 “Highlight”,区分大小写。
          $("p").highlight("light", { wordsOnly: true });      //高亮段落中的 “light”,必须是完整单词,“highlight”中的“light”不会高亮
          $("p").highlight("highlight", { element: ‘em‘, className: ‘error‘ });      //高亮段落中的 “hightlight”,并为其指定父标签为em,class为error
          $("p").highlight("highlight", { element: ‘em‘, className: ‘error‘ });      //高亮段落中的 “hightlight”,并为其指定父标签为em,class为error
         $("body p").highlight("jQuery", { element: ‘a‘, className: ‘jQueryLink‘});     $("body p a.jQueryLink").attr({ href: ‘http://jquery.com‘ });    //高亮段落中的 “jQuery”,并为其加上链接。
  • 时间: 2024-08-29 06:53:22

    高亮显示搜索到的关键字-jquery.highlight.js的用法!的相关文章

    jquery.cycle.js简单用法实例

    样式: a{text-decoration: none;} *{margin:0; padding:0;} /*容器设置*/ .player { width:216px; height:248px; background:url(http://i2.itc.cn/20120117/2cc0_da8f6c82_c8da_693d_7714_9533a013006a_3.jpg) no-repeat; background-color:#ede1d1; position:relative; padd

    jQuery.Form.js 的用法

    jQuery.Form.js 插件的作用是实现Ajax提交表单. 方法: 1.formSerilize() 用于序列化表单中的数据,并将其自动整理成适合AJAX异步请求的URL地址格式. 2.clearForm() 清除表单中所有输入值的内容. 3.restForm 重置表单中所有的字段内容.即将所有表单中的字段恢复到页面加载时的默认值. 疑问:ajaxForm()与ajaxSubmit()的区别: 答案:$("#form1").ajaxForm(); 相当于以下两行: $("

    看看该死的jquery.form.js的用法,不是个东西

    $("#btnReg").click(function () { var options = { //target: '#output', // 把服务器返回的内容放入id为output的元素中 beforeSubmit: showRequest, // 提交前的回调函数 success: showResponse, // 提交后的回调函数 url: config.handles.form, //默认是form的action,如果申明,则会覆盖 type: "post&quo

    JQuery.upload.js的用法简介

    用法很简单,效果却很棒,先看看效果图 是不是很简洁清爽,后台Java文件接收类就不贴了,都是一样的. 注意:为了简洁的接收上传文件的相关信息,后台凡是在涉及上传文件的实体类,都增加字段'upload'.1.HTML部分:只需添加一行就行: <div class="upload" id="case" name="case" action="/userinfo/upload" data-num="2" d

    jquery.min.js v1.10.3版本autocomplete方法会在text前添加搜索出多少项的文本信息 要去除

    http://stackoverflow.com/questions/13011127/how-to-remove-change-jquery-ui-autocomplete-helper-text How to remove/change JQuery UI Autocomplete Helper text? It seems that this is a new feature in JQuery UI 1.9.0, because I used JQuery UI plenty of ti

    jquery.validation.js 表单验证

    jquery.validation.js 表单验证 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 一导入js库 <script src="../js/jquery.js" type="text/javascript"></script> <script src="../js/jq

    jQuery验证控件jquery.validate.js使用说明+中文API

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html 一导入js库<script src="../js/jquery.js" type="text/javascript"></script>

    jQuery验证空间jquery.validate.js使用说明+中文API

    --------转载自http://www.cnblogs.com/hejunrex/archive/2011/11/17/2252193.html jQuery plugin: Validation 使用说明 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 转载自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html 一.导入js库 <script src=

    jQuery验证控件jquery.validate.js使用说明+中文API - Rex.He - 博客园

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html 一导入js库 <script src="../js/jquery.js" type="text/javascript"></script>