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

Using jQuery.get() to load content from another page

本文关键字:加载 另一个 jQuery get 使用      更新时间:2023-09-26

我试图使一个请求从另一个页面加载内容,然后通过该内容搜索并拉出我需要的信息。我一直在尝试使用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" );

试试这个

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
});