搜索结果新增简易高亮

This commit is contained in:
yangjian 2021-03-06 10:39:36 +08:00
parent 6a915d09ca
commit 9b0fdc5323
3 changed files with 39 additions and 2 deletions

View File

@ -341,3 +341,40 @@ textBecomeImg = function(text,fontsize,fontcolor){
### 文档阅读页面JavaScript函数和变量定义 ###
########################################################
*/
// URL参数解析
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
// 搜索词高亮
function keyLight(id, key, bgColor){
// console.log(id,key,decodeURI(key))
key = decodeURI(key);
var oDiv = document.getElementById(id),
sText = oDiv.innerHTML,
bgColor = bgColor || "#c00",
sKey = "<span name='addSpan' style='color: "+bgColor+";background:ff0;'>"+key+"</span>",
num = -1,
rStr = new RegExp(key, "ig"),
rHtml = new RegExp("\<.*?\>","ig"), //匹配html元素
aHtml = sText.match(rHtml); //存放html元素的数组
sText = sText.replace(rHtml, '{~}'); //替换html标签
// sText = sText.replace(rStr,sKey); //替换key
sText = sText.replace(rStr,function(text){
return "<span name='addSpan' style='color:#333;background:#ff0;'>"+text+"</span>"
}); //替换key
sText = sText.replace(/{~}/g,function(){ //恢复html标签
num++;
return aHtml[num];
});
oDiv.innerHTML = sText;
};
keyLight('doc-content',getQueryVariable("highlight"))

View File

@ -187,7 +187,7 @@
<div id="wm"></div>
{% endif %}
<!-- 文档内容 -->
<div class="doc-content">
<div class="doc-content" id="doc-content">
<!-- 标题 -->
<div class="doc-info">
<!-- 页面主体头信息 -->

View File

@ -187,7 +187,7 @@
<div style="margin-bottom: 18px;">
<!-- 标题 -->
<h3>
<a href="{{ result.object.get_absolute_url }}" target="_blank" class="search_result_title">{% highlight result.object.name with query %}</a>
<a href="{{ result.object.get_absolute_url }}?highlight={{query}}" target="_blank" class="search_result_title">{% highlight result.object.name with query %}</a>
</h3>
<!-- 简介 -->
{% highlight result.object.pre_content with query %}