mark.js文章关键字高亮插件使用

JavaScript015

mark.js文章关键字高亮插件使用,第1张

<html>

<input type="hidden" name="keyword" value="<?php echo $result->highlight_keywords ?>">

<div class="sec_main">

     <?phh echo $result->content?>

</div><!-- section main end -->

<js>

<script src="/static/js/jquery.mark.min.js"></script>

<script>

   $(function() {

       var keyword = $("input[name='keyword']").val()

       var options = {

           "className": "match"

       }

       $(".sec_main").mark(keyword,options)

   })

</script>

高亮文字的样式设置

<css>

.match{

     color:red

}

var oldKey=""

function showKey(allStr,keyStr){

if(oldKey!=""){

allStr.replace("<font color='#ff0000'>"+oldKey+"</font>",oldKey)

}

if(keyStr!=""){

allStr.replace(keyStr,"<font color='#ff0000'>"+keyStr+"</font>")

}

oldKey = keyStr

}