问题
I'm using asp.net mvc to display a .html document that I know exactly what it contains.
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.
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.
I'm using VS2015 Enterprise
回答1:
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>
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>");
});
And here's a sample fiddle.
来源:https://stackoverflow.com/questions/43556535/find-a-sentence-in-an-html-page-displayed-in-the-browser-and-highlight-it