javascript – 如何从html中获取元素作为字符串

前端之家收集整理的这篇文章主要介绍了javascript – 如何从html中获取元素作为字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个愚蠢的问题. jQuery.ajax请求将完整的 HTML文本作为字符串返回给我.我在服务器上出现错误时收到此类响应.服务器给我一个错误描述,我想放在当前页面的相应位置.

所以现在问题是:我有一个包含完整HTML文档的字符串(不是XML !!!请参阅< hr>元素内部).我需要只有BODY部分作为jQuery对象.然后我可以将它附加到我页面的相应部分.@H_502_3@

这是我需要解析的字符串示例:@H_502_3@

<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)
-->

解决方法

必须拥有的非jQuery答案:
var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(entirePageHTML)[1];

这将仅返回body标签内的内容.@H_502_3@

UPDATE它接受body标签上设置的属性@H_502_3@

原文链接:https://www.f2er.com/js/156267.html

猜你在找的JavaScript相关文章