PageAdmin网站内容管理系统(CMS)交流论坛

注册

 

QQ登录

只需一步,快速开始

发新话题 回复该主题

搜索结果关键词高亮怎么设置 [复制链接]

1#
如题,搜索结果关键词高亮怎么设置,求大佬指教
分享 转发
TOP
2#

发你的搜素模板内容看看,哪里需要高亮显示?
TOP
3#

回复 2楼xiyou的帖子
  1. @{
  2.     int columnId = Html.CurrentColumnId();
  3.     string kw = StringHelper.Format<string>(HttpContext.Current.Request.QueryString["kw"]);
  4.     string[] pagePathArr = Html.PagePath().Split('/');
  5.     int currentPage = StringHelper.Format<int>(Html.CustomViewParameter("page"));
  6.     
  7.     
  8.     if (currentPage <= 0)
  9.     {
  10.         currentPage = 1;
  11.     }
  12.     PageInfo pageInfo = new PageInfo()
  13.     {
  14.         PageSize = 10,//每页显示10条数据
  15.         CurrentPage = currentPage, //当前页码,系统预设
  16.     
  17.     };
  18.     var dataList = Html.InfoDataList(new { Table = "news" ,OrderBy="thedate desc"},"title like @kw", new { kw = "%" + kw + "%" }, pageInfo);
  19.     ViewBag.Path = "/news/Search/"; //PagebreakPartial分页文件用到
  20. }
  21. <div class="container padding-bottom-60">
  22.     <ul class="breadcrumb">
  23.         <li><a href="@Html.SiteUrl()"><span class="fa fa-home"> 首页</span></a></li>
  24.         <li><a href="">文章搜索</a></li>

  25.     </ul>
  26.     <ul class="news-list padding-top-10">
  27.              <li style="border-bottom: 0px solid #dddbdb;">
  28.           <span>搜索</span>
  29.           <span style="color:#db3636">"@kw"</span>
  30.           <span>共找到</span>
  31.           <span style="color:#db3636">"<%=RecordCount%>"</span>
  32.           <span>篇文章</span>
  33.         </li>
  34.         @{int num = 0;
  35.             foreach (var item in dataList)
  36.             {
  37.                 num++;
  38.                 string url = Html.InfoDataUrl((int)item.ColumnId, (int)item.Id);
  39.                 string date = item.Thedate.ToString("yyyy-MM-dd");

  40.                 <li class="item">
  41.                     <span class="title" style="background: url([img]http://129.0.12.91/Templates/CompanyDefault/images/5-1.png[/img]) no-repeat left center;">
  42.                         <a href="@url" target="_blank" title="@item.Title">@item.Title</a>
  43.                     </span>
  44.                     <span class="date">@date</span>
  45.                 </li>
  46.             }
  47.             if (num == 0)
  48.             {
  49.             
  50.                 <li class="line-height-2 padding-bottom-20 padding-top-20" style="height: 300px;margin-left:500px;border-bottom: 0px solid #dddbdb;">
  51.             <img class="img4" referrerpolicy="no-referrer" src="http://129.0.12.91/Templates/CompanyDefault/images/not found2.png">
  52.                     <span class="word11">暂时没有找到匹配的记录</span>
  53.             <span class="info7">请更换一个关键词重新搜索!</span>
  54.                 </li>
  55.             }
  56.         }
  57.     </ul>
  58.      @Html.Partial("PagebreakPartial", pageInfo)
  59. </div>
  60. <script>
  61.     $("#input-search").val("@kw");
  62.     //$searchBtn.trigger("click");//触发搜索按钮,显示搜索框
  63. </script>
复制代码
搜索关键词高亮
TOP
4#

<a href="@url" target="_blank" title="@item.Title">@item.Title</a>
改为
<a href="@url" target="_blank" title="@item.Title">@(item.Title.Replace(kw,"<strong>"+kw+"</strong>"))</a>

然后自己加个css样式控制strong标签高亮就可以了
TOP
5#

回复 4楼xiyou的帖子

好的!多谢!
TOP
6#

回复 4楼xiyou的帖子



为什么是这样的效果
TOP
7#

回复 4楼xiyou的帖子

拜托拜托,再帮忙看看吧
TOP
8#

替换后最外层加一个Html.Raw(),这个方法作用是禁止html转义
TOP
9#

回复 8楼xiyou的帖子

加到哪儿呀
TOP
10#

@Html.Raw(item.Title.Replace(kw,"<strong>"+kw+"</strong>")) 试试
TOP
11#

回复 10楼青玄科技的帖子

哦哦,好了!谢谢谢谢么么哒
TOP
12#

做个记号,有用
TOP
13#

回复 2楼xiyou的帖子

请问我的搜索页面模板里,如果想加入“关键字”为黄色背景,应该在哪里如何修改?
  1. @{
  2.     PageInfo pageInfo = new PageInfo()
  3.     {
  4.         PageSize = 10,//每页显示10条数据
  5.         CurrentPage = (int)ViewBag.CurrentPage //当前页码,系统预设
  6.     };
  7.     string kw = StringHelper.Format<string>(Request.QueryString["kw"]);
  8. }
  9. <section id="subcont">
  10.     @Html.Partial("SubBannerPartial")
  11.     <div class="wrapper ">
  12.         <div class="clearfix">
  13.             @Html.Partial("leftNavPartial")
  14.         </div>
  15.         <div class="c12 location mob_none">
  16.             @Html.Partial("BreadcrumbPartial")
  17.         </div>
  18.         <!--预算类新闻列表-->
  19.         <div class="c12 subnews_s">
  20.             <ul>
  21.                 @Html.Partial("NewsListModel", Html.InfoDataList(new { Table = "news" }, "title like @title", new { title = "%" + kw + "%" }, pageInfo))
  22.             </ul>
  23.             <div id="loading">
  24.                 <section>
  25.                     <div class="sk-three-bounce">
  26.                         <div class="sk-bounce-1 sk-child"></div>
  27.                         <div class="sk-bounce-2 sk-child"></div>
  28.                         <div class="sk-bounce-3 sk-child"></div>
  29.                     </div>
  30.                 </section>
  31.             </div>
  32.         </div>
  33.         <!--预算类新闻列表END-->
  34.     </div>
  35.     <div class="subtopbg"> </div>
  36. </section>
  37. <script type="text/javascript">
  38.         var state = 0;
  39.         var CurrentPage = 1;
  40.         var $ul = $(".subnews_s").children("ul");
  41.         var liLen = $ul.children("li").length;
  42.         var $loading = $("#loading");
  43.         $loading.data("on", true).hide();
  44.         $(window).scroll(function () {
  45.             if (!$loading.data("on")) {
  46.                 setTimeout(function () { $loading.hide(); }, 800);
  47.                 return;
  48.             }
  49.             if (@pageInfo.RecordCount> liLen && ($(window).scrollTop() + $(window).height()) >= ($(document).height() - 200))
  50.             {
  51.                 CurrentPage++;
  52.                 $loading.data("on", false).fadeIn(800);
  53.                 var json = Ajax({ type: "get", async: false, dataType: "html", url: "/SearchHtml/?model=NewsListModel&title=@(Request.QueryString["kw"])&PageSize=@(pageInfo.PageSize)&CurrentPage=" + CurrentPage, }, function (data) {
  54.                     var time = setTimeout(function () {
  55.                         $ul.append(data);
  56.                         liLen = $ul.children("li").length;
  57.                         $loading.data("on", true).fadeOut();
  58.                         sweetTitles.init();
  59.                         clearTimeout(time);
  60.                     }, 800)
  61.                 });
  62.             }
  63.             if (@pageInfo.RecordCount==liLen && ($(window).scrollTop() + $(window).height()) >= ($(document).height() - 200)) {
  64.                 if ($loading.data("on")) {
  65.                     Msg("已加载完毕!");
  66.                     $loading.data("on", false);
  67.                 }
  68.             }
  69.         });
  70. </script>
复制代码
TOP
14#

/SearchHtml这个路由的模板文件修改找,感觉是通过自定义路由做的。
TOP
15#

  1. <a href="@url" target="_blank" title="@item.Title">@Html.Raw(item.Title.Replace(kw,"<strong>"+kw+"</strong>"))</a>

  2. <span style="background-color: #FFFF00;"></span>
复制代码
TOP
16#

回复 14楼xiyou的帖子
  1. @{
  2.     Layout = null;
  3.     int pageSize = StringHelper.Format<int>(Request.QueryString["PageSize"]);
  4.     string title = StringHelper.Format<string>(Request.QueryString["Title"]);
  5.     string model = StringHelper.Format<string>(Request.QueryString["model"]);
  6.     PageInfo pageInfo = new PageInfo()
  7.     {
  8.         PageSize = pageSize,//每页显示10条数据
  9.         CurrentPage = StringHelper.Format<int>(Request.QueryString["CurrentPage"]) //当前页码,系统预设
  10.     };
  11. }
  12. @Html.Partial(model, Html.InfoDataList(new { Table = "news" }, "title like @title", new { title = "%" + title + "%" }, pageInfo))
复制代码
TOP
发新话题 回复该主题