使用jQuery.get()从另一个页面加载内容

时间:2014-07-18 10:00:27

标签: javascript jquery ajax

我正在尝试发出一个请求从另一个页面加载内容,然后搜索该内容并提取我需要的信息。我一直在尝试使用jQuery.get()来做到这一点,但无法让它工作。

以下是我正在使用的代码:

$.get('/category/page-2/', function(data) {
  var theContent = $(data).find('#newWrapper img').attr('src');
  theContent.appendTo('#container'); // this isn't inserting the url string

  console.log('the content: ' + theContent); // theContent returns undefined
}, 'html');

我有jQuery.load()工作,但我需要从返回的文档中获取多个信息,并且不想为每个文档发出请求。这很好用:

$( ".related-product" ).eq(0).load( "/category/page-2/ #newWrapper img" );

1 个答案:

答案 0 :(得分:0)

试试这个

jQuery.get('/category/page-2/',
{
    ajax: true
}, function (data) {
    data = jQuery(data);
    var theContent = jQuery('#newWrapper img', data).attr('src');
    theContent.appendTo('#container'); // this isn't inserting the url string
    console.log('the content: ' + theContent); // theContent returns undefined

});