为什么我没有从其他页面获取内容? jQuery $ .get函数

时间:2014-08-06 10:57:24

标签: javascript jquery html html5

我试图从div #content

中获取 page1.html 中的内容
$(function(){
    $.get('page1.html', function(data){
        console.log($("#content",data).html()); <-- console.log says undefined
    });
});

Page1.html

<div id="content">
    <h1>Page 1</h1>
    <h2>Page 1 Content</h2>
</div>

我不确定为什么它不起作用,console.log会出现 undefined

我跟着这里: get content inside a div inside a data object

1 个答案:

答案 0 :(得分:0)

以下代码将执行此操作:

var url = 'page1.html';

$(function() {
  $.get(url, function(data) {
    console.log(data);
    $('#content').html(data);
  });
});

使用httpbin.org小提琴:http://jsfiddle.net/klenwell/kyu6g3e0/(在Chrome中测试)