【问题标题】:Use jQuery on a variable instead on the DOM ?在变量上使用 jQuery 而不是在 DOM 上?
【发布时间】:2010-05-07 06:56:34
【问题描述】:

在 jQuery 中你可以这样做:

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

我想编写一个 jQuery 函数,它从网站抓取 x 级别并收集所有 href 到 gif 图像。

所以当我使用 get 函数检索另一个页面时,

$.get(href, function(data) {

});

我希望能够做类似的事情

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

这可能吗?

...更新...

多亏了答案,我才能解决这个问题。

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

有关如何使用它的示例,请参阅this zip file。

【问题讨论】:

    标签: jquery web variables hyperlink web-crawler


    【解决方案1】:

    您可以将接收到的数据放入 DOM 中,然后运行

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

    类似的东西

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

    【讨论】:

      【解决方案2】:

      我认为最好在客户端(即 PHP)上进行抓取并将图像 href 返回到 javascript/jquery。

      哦,data.$("a[href$='.img']") 是不可能的。 jQuery() 通过搜索当前存在的 DOM 来工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-14
        • 2011-10-01
        • 2015-01-25
        • 1970-01-01
        • 2016-03-10
        • 2018-01-01
        • 2021-02-08
        • 1970-01-01
        相关资源
        最近更新 更多