[英]How get <body> element from the html which one have as a string
我有一個愚蠢的問題。 一個jQuery.ajax
請求以字符串形式返回一個完整的 HTML 文本。 在服務器出現錯誤的情況下,我會收到此類響應。 服務器給了我一個錯誤描述,我想把它放在我當前頁面的相應位置內。
所以現在的問題是:我有一個包含完整 HTML 文檔的字符串(這不是 XML !!!請參閱里面的<hr>
元素)。 例如,我只需要將 BODY 部分作為 jQuery 對象。 然后我可以將它附加到我頁面的相應部分。
這是我需要解析的字符串示例:
<html>
<head>
<title>The resource cannot be found.</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
// ...
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>The resource cannot be found.</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>HTTP 404. The resource you are looking for ...bla bla....
<br><br>
<b> Requested URL: </b>/ImportBPImagesInfos/Repository.svc/GetFullProfilimageSw<br><br>
<hr width=100% size=1 color=silver>
<b>Version Information:</b> Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
</font>
</body>
</html>
<!--
[HttpException]: A public action method '....
at System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->
以及必備的非 jQuery 答案:
var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(entirePageHTML)[1];
這將只返回 body 標簽內的內容。
UPDATE這接受在 body 標簽上設置的屬性
不使用 jQuery 的另一種方法:
function getStupidErrorMessage(str) {
var bodyTags = str.match(/<\/*body[^>]*>/gim);
// returns an array
// bodyTags[0] is body open, bodyTags[1] is body close
// unless someone output the markup backwards :)
bodyContents = str.slice(bodyTags[0].length,-(bodyTags[1].length));
return bodyContents; // use as innerHTML of <body>
}
如果您需要 BODY 標記的屬性,也請解析這些屬性。
如果出現錯誤,您可以將整個HTML字符串傳遞給jQuery以構建它的內部表示:
var bodyHtml = $(entirePageHTML).find('body').html();
要么
var errorMessage = $(entirePageHTML).find('body h1').text();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.