Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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 从jquery对象提取div html_Javascript_Jquery_Html - Fatal编程技术网

Javascript 从jquery对象提取div html

Javascript 从jquery对象提取div html,javascript,jquery,html,Javascript,Jquery,Html,这可能很简单,但我从来没有这样做过。我正在加载站点上的另一个页面,并希望从id为terms\u text的div中提取html 因此,我正在加载数据(没有问题),并尝试对其进行过滤,但收到$html变量的未定义数据。有什么想法吗 function test () { $.get("/terms?" + new Date().getTime(), function(data){ var $html = $(data).filter('#terms_text').html();

这可能很简单,但我从来没有这样做过。我正在加载站点上的另一个页面,并希望从id为terms\u text的div中提取html

因此,我正在加载数据(没有问题),并尝试对其进行过滤,但收到$html变量的未定义数据。有什么想法吗

 function test () {
 $.get("/terms?" + new Date().getTime(), function(data){
     var $html = $(data).filter('#terms_text').html();
     alert($html);
});
}

您可能需要使用
$(数据)。查找('terms_text')
而不是
$(数据)。筛选('terms_text')
。要么是这样,要么就是在id为
terms\u text的
/terms…
中没有返回元素,这很好!我使用了
var$html=$(data).find(“#terms_text”)
然后打印它,
$html.html()
这是完全相同的,我的意思是使用
find
而不是
filter
。在对其执行操作之前,您可能希望对其进行空检查。您可以使用
Date.now()
而不是
new Date().getTime()