Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.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 如何通过ajax-jQuery获得一个标记的值而不是整个页面的值_Javascript_Jquery_Ajax_Html - Fatal编程技术网

Javascript 如何通过ajax-jQuery获得一个标记的值而不是整个页面的值

Javascript 如何通过ajax-jQuery获得一个标记的值而不是整个页面的值,javascript,jquery,ajax,html,Javascript,Jquery,Ajax,Html,如果我有这个页面page.html: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Page</title> </head> <body> <h1>Hello World!!</h1> </body> </html> 如何获得的值而不是整页?您可以将结果转换为jQuery对

如果我有这个页面
page.html

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Page</title>
</head>

<body>

<h1>Hello World!!</h1>
</body>
</html>

如何获得
的值而不是整页?

您可以将结果转换为jQuery对象

$.ajax({

  url:"page.html",
  success: function(data){
    var $html = $(data),
        $body = $html.find('body');
  }

});

$(数据).find('body')
$body
是一个对象吗?是的,它是一个jquery对象。您可以使用
$body.html()
获取其内容;我得到了未定义的值检查变量
数据的值
。如果它是空的,那么ajax调用中就有问题。请记住,变量
$body
的范围是
success
回调函数。
$.ajax({

  url:"page.html",
  success: function(data){
    var $html = $(data),
        $body = $html.find('body');
  }

});