代码之家  ›  专栏  ›  技术社区  ›  Oleg

如何从HTML中获取作为字符串的<body>元素

  •  6
  • Oleg  · 技术社区  · 14 年前

    我有个愚蠢的问题。安 jQuery.ajax 请求返回我A 完全HTML文本 作为字符串。在服务器出错的情况下,我会收到这样的响应。服务器给了我一个错误描述,我想把它放在当前页面的相应位置中。

    所以现在的问题是:我有一个字符串包含完整的HTML文档(它不是XML!!!!看见 <hr> 元素内部)。例如,我只需要将主体部分作为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>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
    
        </font>
    
      </body>
    </html>
    <!--
    [HttpException]: A public action method &#39;....
       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)
    -->
    
    3 回复  |  直到 14 年前
        1
  •  16
  •   Sean Kinsey    14 年前

    必须有非jquery答案:

     var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(entirePageHTML)[1];
    

    这将只返回身体标签中的内容。

    更新 它接受body标记上设置的属性

        2
  •  4
  •   Robusto    14 年前

    另一种不使用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标记的属性,也可以解析这些属性。

        3
  •  1
  •   David Hedlund    14 年前

    在出现错误的情况下,可以将整个HTML字符串传递给jquery以构建其内部表示形式:

    var bodyHtml = $(entirePageHTML).find('body').html();
    

    var errorMessage = $(entirePageHTML).find('body h1').text();