简体   繁体   English

在变量上使用jQuery而不在DOM上使用jQuery?

[英]Use jQuery on a variable instead on the DOM ?

In jQuery you can do : 在jQuery中,您可以执行以下操作:

$("a[href$='.img']").each(function(index) {
    alert($(this).attr('href'));
}

I want to write a jQuery function which crawls x-levels from a website and collects all hrefs to gif images. 我想编写一个jQuery函数,该函数从网站上爬取x级别并将所有href收集到gif图像。

So when I use the the get function to retrieve another page, 因此,当我使用get函数检索另一个页面时,

$.get(href, function(data) {

});

I want to be able to do something like 我希望能够做类似的事情

data.$("a[href$='.img']").each(function(index) {
});

Is this possible ? 这可能吗 ?

...UPDATE... ...更新...

Thanks to the answers, I was able to fix this problem. 多亏了答案,我才得以解决此问题。

function FetchPage(href) {
    $.ajax({
        url: href,
        async: false,
        cache: false,
        success: function(html){
            $("#__tmp__").append("<page><name>" + href + "</name><content>" + html + "</content></page>");
        }
    });
}

See this zip fil e for an example how to use it. 有关如何使用它的示例,请参见此zip文件

you could put recived data into the DOM and then run 您可以将获取的数据放入DOM中,然后运行

$("a[href$='.img']").each(function(index) {
    alert($(this).attr('href'));
}

something like this 像这样的东西

$.get(href, function(data) {
  $("#somelement").hide();
  $("#somelement").html(data);
  $("#somelement").find("a[href$='.img']").each(function(index) {
    alert($(this).attr('href'));
  }
  $("#somelement").show();
}

I think it's better do the crawling on the client-side (ie PHP) instead and return the image hrefs back to javascript/jquery. 我认为最好在客户端(即PHP)上进行爬网,然后将图像hrefs返回到javascript / jquery。

Oh, and data.$("a[href$='.img']") is not possible. 哦,而且data。$(“ a [href $ ='。img']”)是不可能的。 jQuery() works by searching through the currently existing DOM. jQuery()通过搜索当前存在的DOM来工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM