获取特定元素从外部网站使用jQuery / AJAX [英] Getting specific element from external site using jQuery / ajax

查看:152
本文介绍了获取特定元素从外部网站使用jQuery / AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery和 这个插件 可以从外部站点拉内容。

I am using jQuery and this plugin to pull content from an external site.

它的伟大工程使用下面的code:

It works great using the following code:

$.ajax({
       url: 'http://www.somesite.com/',
       type: 'GET',
       success: function(res) {
          $("#here").html(res.responseText);
       }
     });

但我真的很想做的,只是拉从目标网站部分(DIV),我的理解使用 load()的时候,这是唯一可能的方法,而不是 GET

我后来发现的 这code ,但是当我试图将其建成了code,它似乎没有工作,因为这样的:

I then found this code , but when I tried to build it into the code, it didn't seem to work, as such:

$.ajax({
       url: 'http://www.somesite.com/',
       type: 'GET',
       success: function(res) {
          $(res).find('div.content').each(function(){
              $('#here').append($(this).html());
         });

       }
     });

展望对萤火虫的请求,该请求似乎成功,但code似乎无法找到以下标签:< D​​IV CLASS =内容的行> 目标站点上。

我需要确保目标元素,而不是一类的#ID,?

Do I need to make sure the target element has an #ID, instead of a class?

谢谢!

推荐答案

在你原有的成功回调的responseText的从获取你的HTML内容,你应该在第二种情况下做同样的:

In your original success callback your fetching your html contents from responseText, you should do the same in the second case:

   success: function(res) {
      $(res.responseText).find('div.content').each(function(){
          $('#here').append($(this).html());
     });

使用类=内容或ID应该工作。但是,你应该知道,code以上将所有的 DIV 的的是具有类的内容到你的ID =点击这里分区,这可能不是你想要的。如果你想有一个特定元素的内容之外,使用它的 ID 的替代。

Using class="content" or id should work. However, you should be aware that the code above adds all div's that have class content to your id="here" div, which might not be what you want. If you want just the contents of a specific element, use its id instead.

这篇关于获取特定元素从外部网站使用jQuery / AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆