javascript - 相同的词正在消失
问题描述
我想突出显示一个特定的单词并从 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);
}
解决方案
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()
}
});
推荐阅读
- angularjs - 如何在谷歌实例上安装 node.js 和 angular.js
- javascript - 当测试用python(pytest)编写时,有没有办法让nodejs代码覆盖?
- android - 我的 ListView 在 android 模拟器中不可见,我使用了线性布局
- linux - 为什么 gcc 列出了与我实际的 cpu 架构不同的行进?
- firebase - 在 Firebase 上为 Vue JS 2 应用程序配置托管
- angular - 在`中追加“%”
- ios - 如何在 iPhone SE 和 5S 上设置自动布局?
- visual-studio - 如何轻松更改 XAML 代码样式(仅列出一行)
- drake - 逆运动轨迹上的速度级约束
- c++ - 为什么我们不能在 C++ 中初始化一个类的成员变量