在浏览器中显示的html页面中找到一个句子并突出显示它 [英] Find a sentence in an html page displayed in the browser and Highlight it
问题描述
我在我的控制器中使用了这个方法:
I'm using asp.net mvc to display a .html document that I know exactly what it contains.
我使用asp.net mvc来显示一个.html文档, p>
I have this method in my controller:
public ActionResult GetHtmlPage(string path)
{
return new FilePathResult(path, "text/html");
}
我有这个简单视图:
and I have this Simple view:
@{
ViewBag.Title = "ManageDocument";
}
<h2>ManageDocument</h2>
@Html.Action("GetHtmlPage", "myController", new { path = Model.documentPath })
文档显示没有问题。但我想突出显示特定句子,我知道它存在于文档中的某处。
The document is displayed with no problems. But i want to Highlight a specific sentence that I know it exists somewhere in the document.
我的意思是,我试图实现一个JavaScript代码也许找到我想在文档中突出显示的特定句子并突出显示它!
我在JavaScript中阅读了关于window.find()的内容,但我的asp.net解决方案似乎无法识别它。
What I mean is, I'm trying to implement a JavaScript code maybe to find that specific sentence i want to highlight in the document and highlight it! I read about window.find() in JavaScript but my asp.net solution doesn't seem to recognize it.
我正在使用VS2015 Enterprise
I'm using VS2015 Enterprise
推荐答案
用一些标签包装文本并应用您可能需要的任何自定义样式。假设您有以下标记:
You could use jQuery to wrap the text with some tags and apply whatever custom styles you might need. Let's suppose that you have the following markup:
<div>
This is some specific sentence
</div>
并且您希望以此结束:
and you want to end up with this:
<div>
This is <strong>some specific</strong> sentence
</div>
您可以尝试:contains
选择器找到所需的文本部分,然后用一些标签包装:
You could try the :contains
selector to locate the desired portion of the text and then wrap it with some tags:
$("div:contains('some specific')").html(function(_, html) {
return html.replace(/(some specific)/g, "<strong>$1</strong>");
});
以下是 sample fiddle
。
这篇关于在浏览器中显示的html页面中找到一个句子并突出显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!