Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得<;车身>;元素,其中一个作为字符串_Javascript_Jquery_Dom - Fatal编程技术网

Javascript 如何获得<;车身>;元素,其中一个作为字符串

Javascript 如何获得<;车身>;元素,其中一个作为字符串,javascript,jquery,dom,Javascript,Jquery,Dom,我有个愚蠢的问题。ajax请求将HTML全文作为字符串返回给我。如果服务器出现错误,我会收到这样的响应。服务器给了我一个错误描述,我想把它放在当前页面的相应位置 现在的问题是:我有一个字符串包含完整的HTML文档(它不是XML!!!请参阅里面的元素)。例如,我只需要将身体部分作为jQuery对象。然后我可以将其附加到页面的相应部分 下面是我需要解析的字符串示例: <html> <head> <title>The resource cannot be

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

现在的问题是:我有一个字符串包含完整的HTML文档(它不是XML!!!请参阅里面的

元素)。例如,我只需要将身体部分作为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)
-->

找不到资源。
正文{字体系列:“Verdana”;字体重量:正常;字体大小:.7em;颜色:黑色;}
p{字体系列:“Verdana”;字体大小:正常;颜色:黑色;页边顶部:-5px}
// ...
“/”应用程序中出现服务器错误。
找不到资源。 描述:HTTP404。你正在寻找的资源…等等。。。。

请求的URL:/ImportBPImagesInfos/Repository.svc/GetFullProfilimageSw

版本信息:Microsoft.NET Framework版本:4.0.30319;ASP.NET版本:4.0.30319.1
不使用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> 
}
函数getStupidErrorMessage(str){
var bodyTags=str.match(/]*>/gim);
//返回一个数组
//bodyTags[0]为主体打开,bodyTags[1]为主体关闭
//除非有人向后输出标记:)
bodyContents=str.slice(bodyTags[0].length,-(bodyTags[1].length));
return bodyContents;//用作的innerHTML
}

如果您需要BODY标签的属性,也可以解析这些属性。

以及必须具备的非jQuery答案:

 var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(entirePageHTML)[1];
var bodyHtml=/([\s\s]*)/.exec(entirePageHTML)[1];
这将只返回body标签中的内容


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

谢谢您的建议,但是我在这个exprestion的第一个和最后一个版本中只收到例外。请仔细查看响应字符串的
body
标签。您的模式将与开始标记不匹配。是否有不使用正则表达式的方法来执行此操作?谢谢您的建议。这个想法很好,但是
bodyTags[0]。长度
bodyTags[1]。长度
不能用于
str.slice
。它们生成错误的子字符串
bodyTags.lastIndex
可以作为
str.slice
的最后一个参数,但是我还没有找到第一个参数的正确值。OK!data.responseText.slice(str.indexOf(bodyTags[0]),bodyTags.lastIndex)工作正常@奥列格:很高兴它能为您工作,但在我看来str.indexOf(bodyTags[0])只会返回0。那么,你不会得到包括打开标签在内的内容吗?我以为你不想那样。
str.match(/]*>/gim)
在我的测试字符串
str
上生成数组,其中
bodyTags[0]
'
,而
bodyTags[1]
'
。因此
str.indexOf(bodyTags[0])+bodyTags[0]。length
为我提供了
str
bodyTags内部
后包含的索引。lastIndex
str.lastIndexOf(bodyTags[0])
str lastIndexOf('')相同。所以它起作用了。现在我有两个版本的作品。我选择Sean Kinsey的
exec
版本,因为它不区分大小写,并且在一条语句中生成正文内容。不过,非常感谢。问得好。仍然没有好的答案。