为什么我只用$('a')用jQuery获得一个元素数组,而不是全部? [英] Why do I get only one element array by $('a') with jQuery instead of all of them?

查看:162
本文介绍了为什么我只用$('a')用jQuery获得一个元素数组,而不是全部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,以对不使用jQuery的论坛的每个线程内容进行爬网.因此,我添加了js代码以加载jQuery(它将最终显示出来).

I'm writing a script to crawl each thread content of a forum that is not using jQuery. So I add js code to load jQuery (it will show at last).

当我在Chrome控制台中键入$.fn.jquery并返回'1.11.1'时,它确实可以工作.那我可以方便地使用jQuery吗?

It does work when I type $.fn.jquery in Chrome console and it return '1.11.1'. So can I use jQuery handily?

当然不能.我键入$('a'),它只返回[<a href="index.php?">foo</a>],它在数组中只有 ONE 元素,但是这个论坛确实有越来越多的超链接.

Sure not. I type $('a') and it just only return [<a href="index.php?">foo</a>], it has only ONE element in array, but this forum does has more and more hyperlinks.

已更新:如果我键入$('a[href=bar]')(或其他ID),它将返回[<a href="index.php?">bar</a>].

Updated: If I type $('a[href=bar]')(or some other id) it will return [<a href="index.php?">bar</a>].

我尝试使用其他方式,例如$('*').find('a'),它会返回相同的结果.

I try other way like $('*').find('a') it return the same.

顺便说一句,论坛中只有一个iframe.

BTW, there is only one iframe in the forum.

任何人都有想法或遇到相同的问题吗?

Can anyone has idea or meet same issue?

下面是加载jQuery的js代码.

Below is js code of loading jQuery.

  (function () {

    function loadScript(url, callback) {

        var script = document.createElement("script")
        script.type = "text/javascript";

        if (script.readyState) { //IE
            script.onreadystatechange = function () {
                if (script.readyState == "loaded" || script.readyState == "complete") {
                    script.onreadystatechange = null;
                    callback();
                }
            };
        } else { //Others
            script.onload = function () {
                callback();
            };
        }

        script.src = url;
        document.getElementsByTagName("head")[0].appendChild(script);
    }

    loadScript("https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js", function () {

         //jQuery loaded
         console.log('jquery loaded');

    });

})();

推荐答案

所有人:

我发现了论坛的问题.我在jquery.js中进行调试,并在

I found the problem that the forum has. I debug in the jquery.js and found after

push.apply( results,
    newContext.querySelectorAll( newSelector )

(位于第864行, https://ajax .googleapis.com/ajax/libs/jquery/1.11.1/jquery.js )

results只需在其中推入一个元素,然后从论坛中找到另一个名为common.js的js来定义function push,如下所示:

results just push one element in it, and then I found another js from the forum called common.js define the function push, like this:

Array.prototype.push = function(value) {
    this[this.length] = value;
    return this.length;
}

所以function push做错了事.

这个问题可能不是一个好问题,因为这种情况太特殊了.但是,我学习了如何从js代码进行调试,并且请勿任意覆盖本机函数.

This question may be not a good question, because this situation is too special. But I learn how to debug from the js code and DO NOT override native function arbitrarily.

最后,感谢所有帮助我的人.

Finally, thanks for everyone who helped me.

这篇关于为什么我只用$('a')用jQuery获得一个元素数组,而不是全部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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