【问题标题】:How get <body> element from the html which one have as a string如何从作为字符串的 html 中获取 <body> 元素
【发布时间】:2010-05-18 12:19:29
【问题描述】:

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

所以现在的问题是:我有一个包含完整 HTML 文档的字符串(它不是 XML !!!请参阅内部的 &lt;hr&gt; 元素)。例如,我只需要 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>&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)
-->

【问题讨论】:

  • 好问题。仍然没有好的答案。

标签: javascript jquery dom


【解决方案1】:

以及必备的非 jQuery 答案:

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

这将只返回正文标签内的内容。

UPDATE这接受在body标签上设置的属性

【讨论】:

  • 感谢您的建议,但我只收到此表达式的第一个和最后一个版本的例外
  • 仔细查看响应字符串的body 标记。您的模式与开始标签不匹配。
  • 有没有办法在不使用正则表达式的情况下做到这一点?
【解决方案2】:

不使用 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 标签的属性,也请解析它们。

【讨论】:

  • 感谢您的建议。这个想法很好,但是bodyTags[0].lengthbodyTags[1].length 不能在str.slice 中使用。他们产生错误的子字符串。 bodyTags.lastIndex 可以作为str.slice 的最后一个参数,但是我还没有找到第一个的正确值。
  • 好的! data.responseText.slice(str.indexOf(bodyTags[0]),bodyTags.lastIndex) 工作!
  • @Oleg:很高兴它对你有用,但在我看来 str.indexOf(bodyTags[0]) 只会返回 0。所以你不会得到内容 包括 打开 标签?我以为你不想那样。
  • str.match(/&lt;\/*body[^&gt;]*&gt;/gim) 在我的测试字符串str 上生成数组,其中bodyTags[0]'&lt;body bgcolor="white"&gt;'bodyTags[1]'&lt;/body&gt;'。所以str.indexOf(bodyTags[0]) + bodyTags[0].lengthstrbodyTags.lastIndex 内部的&lt;body bgcolor="white"&gt; 之后给了我包含的索引,与str.lastIndexOf(bodyTags[0])str.lastIndexOf('&lt;/body&gt;') 相同。所以它有效。现在我有两个版本可以工作。我选择了exec 版本的 Sean Kinsey,因为它不区分大小写并在一个语句中生成正文内容。不过还是非常感谢。
猜你喜欢
  • 2019-01-28
  • 2014-09-20
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多