首页 > 解决方案 > 相同的词正在消失

问题描述

我想突出显示一个特定的单词并从 gridview 悬停。我的问题是,如果页面中重复该单词,则该单词会突出显示,但其他相同的单词会从文本中消失。附上高光前后的图像。我附上了突出显示前后缺失单词的图像。请帮忙。

在此处输入图像描述

第1页.aspx

 protected void GridView3_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Pa"] = GridView3.SelectedRow.Cells[4].Text;
            Session["paId"] = GridView3.SelectedRow.Cells[1].Text;
            string sValue = ((HiddenField)GridView3.SelectedRow.FindControl("HiddenField1")).Value;
            Session["panode"] = sValue;
            Session["patt"] = GridView3.SelectedRow.Cells[5].Text.ToString().Trim('\n');
            Response.Redirect("selectipc.aspx");
             
             
        }

page2.aspx

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
                 <script>
                     $(function () {
                         var searchWord = $("#hfWord").val();
                         var iframeHtml = $("#hfHTML").val();
                         var index = iframeHtml.indexOf(searchWord);
                         if (index >= 0) {
                             iframeHtml = iframeHtml.substring(0, index) + "<span id='lblHighlight' style='background-color: yellow;'>"
                             + iframeHtml.substring(index, index + searchWord.length) + "</span>" + iframeHtml.substring(index + searchWord.length);
                         }
                         $('#frmDisplay').contents().find('body').html(iframeHtml);
                         if ($('#frmDisplay').contents().find('#lblHighlight').length > 0) {
                             $('#frmDisplay').contents().find('#lblHighlight')[0].scrollIntoView()
                         }
                     });
</script>

    protected void Page_Load(object sender, EventArgs e)
        {
            //Session.Add("Logintype", Session["new"].ToString());
            if (!this.IsPostBack)
            {
 if (Session["paId"] != null)
                {
                    hfWord.Value = Session["pa"].ToString();
                    getPa();
                    //Session.Contents.Remove(Session["Pa"].ToString());
                }
             }
         }

 public void getPa()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
            string panode = Session["panode"].ToString();
            string pstext = Session["pa"].ToString();
            string patext = Session["patt"].ToString();
            string vc = (Convert.ToInt32(HttpContext.Current.Session["VCN"])).ToString();
SqlCommand cmd = new SqlCommand("select bn.vcFilePath from tblBookNodes bn inner join tbl_Pannotation pa on bn.iModuleId=pa.iNodeId where bn.iModuleId='" + panode + "' ", conn);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    bytesp = (byte[])dr["vcFilePath"];
                    conn.Close();
                }
  getpa();
        }


public void getpa()
        {
            string panId = Session["paId"].ToString();
            string pstext = Session["pa"].ToString();
            string patext = Session["patt"].ToString();
            string fileName = panId.Replace(" ", "_") + ".htm";
            //string returnpath = "File/htmlFile/";
            string strPath = Server.MapPath("~/htmlFile/");
            //if (Directory.Exists(strPath))
            //{
            //    Directory.Delete(strPath, true);
            //}
            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }
            //string pth = strPath;
            string path = strPath + fileName;
            var doc = new HtmlDocument();
            string html = Encoding.UTF8.GetString(bytesp);
            doc.LoadHtml(html);
            StringWriter sw = new StringWriter();
            var hw = new HtmlTextWriter(sw);
            StreamWriter sWriter = new StreamWriter(path);
            sWriter.Write(sw.ToString());
            doc.Save(sWriter);
            sWriter.Close();
            //string fileContents = html;
            string searchText = HttpUtility.HtmlEncode(pstext);
            string newBookmarktag = "<style>font.tip {border - bottom: 1px dashed;text - decoration: none}font.tip:hover {cursor: help;position: sticky}font.tip span {display: none}font.tip:hover span {background-color:F0E6BC;text-align:center;color:Black;font-weight:bold;border: #c0c0c0 1px dotted;padding: 5px 20px 5px 5px;display: block;z - index: 1000;left: 0px;margin: 10px;position: absolute;top: 10px;text - decoration: none}</style><font style=background-color:yellow class=tip><a name=" + pstext + ">" + "<span>" + patext + "</span></a></font>";
             
            // pstext = newBookmarktag;
            html = html.Replace(searchText, newBookmarktag);
            //hfHTML.Value = html;
            System.IO.File.WriteAllText(path, html);
            hfHTML.Value = File.ReadAllText(path);
            //HiddenField1.Value = html;
 
            Page page = (Page)HttpContext.Current.Handler;
            MultiView control = ((IETM.select)(page)).MultiView1;
            View View1 = (View)control.FindControl("View1");
            HtmlControl ctrl = (HtmlControl)View1.FindControl("frmDisplay");
            frmDisplay.Attributes.Add("src", HttpContext.Current.Request.Url + "/.." + "\\htmlFile\\" + fileName);
            MultiView1.SetActiveView(View1);
        }
 

标签: javascriptc#cssasp.net

解决方案


indexOf 只返回第一个值位置,在这种情况下最好使用 split

 $(function () {
     var searchWord = $("#hfWord").val();
     var iframeHtml = $("#hfHTML").val();
     iframeHtml = iframeHtml.split(searchWord).join("<span style='background-color: yellow;' class='lbl-highlight'>"+searchWord+"</span>");
     $('#frmDisplay').contents().find('body').html(iframeHtml);
     if ($('#frmDisplay').contents().find('.lbl-highlight').length > 0) {
       $('#frmDisplay').contents().find('.lbl-highlight')[0].scrollIntoView()
     }
 });

推荐阅读