Javascript 在浏览器中显示的html页面中查找一个句子并高亮显示
我使用asp.NETMVC来显示一个.html文档,我完全知道它包含什么 我的控制器中有此方法:Javascript 在浏览器中显示的html页面中查找一个句子并高亮显示,javascript,jquery,html,asp.net-mvc,frontend,Javascript,Jquery,Html,Asp.net Mvc,Frontend,我使用asp.NETMVC来显示一个.html文档,我完全知道它包含什么 我的控制器中有此方法: public ActionResult GetHtmlPage(string path) { return new FilePathResult(path, "text/html"); } 我有一个简单的观点: @{ ViewBag.Title = "ManageDocument"; } <h2>ManageDocument</h2> @Html.Action("G
public ActionResult GetHtmlPage(string path)
{
return new FilePathResult(path, "text/html");
}
我有一个简单的观点:
@{
ViewBag.Title = "ManageDocument";
}
<h2>ManageDocument</h2>
@Html.Action("GetHtmlPage", "myController", new { path = Model.documentPath })
@{
ViewBag.Title=“ManageDocument”;
}
管理文档
@Action(“GetHtmlPage”,“myController”,新的{path=Model.documentPath})
文档显示时没有问题。但是我想突出显示一个特定的句子,我知道它存在于文档的某个地方
我的意思是,我正在尝试实现一个JavaScript代码,也许是为了找到我想在文档中突出显示的特定句子并突出显示它!
我读过JavaScript中的window.find(),但我的asp.net解决方案似乎无法识别它
我使用的是VS2015 Enterprise,您可以使用jQuery用一些标记包装文本,并应用您可能需要的任何自定义样式。假设您具有以下标记:
<div>
This is some specific sentence
</div>
这里有一个。如果我想在其中插入一个变量该怎么办。假设var sen='some specific'?是什么阻止您在代码中插入变量?只需将硬编码的值替换为一个变量,它很简单:
$(“div:contains(“+sen+”)
。还有:var regex=newregexp(“(“+sen+”),“g”)代码>。最后:返回html.replace(regex,$1)代码>亲爱的先生,我的知识阻止了我:D,lemme tryal,所以不要忘记解释变量可能包含的任何特殊字符,这些字符可能会干扰jQuery选择器和正则表达式。在将其传递给RegExp
构造函数之前,您需要正确地转义它们
<div>
This is <strong>some specific</strong> sentence
</div>
$("div:contains('some specific')").html(function(_, html) {
return html.replace(/(some specific)/g, "<strong>$1</strong>");
});