Skip to content Skip to sidebar Skip to footer

Why Do I Get Only One Element Array By $('a') With JQuery Instead Of All Of Them?

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). It does work when I type $.fn.jquery

Solution 1:

This should do the work

var link = $("a").map(function() {
return this.innerHTML;
}).get();

console.log(link.join());

Put in function what you want to retrieve from html.


Solution 2:

Assuming you are using google chrome here. This seems like an issue where stuff typed in google chrome console window is executing in the context of the parent (or root) page.

Change the context.

See how this is done in jsfiddle.

enter image description here

Changing the context:

enter image description here

Now you can do whatever you want:

enter image description here


Solution 3:

everyone:

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

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

(it is at line 864, https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js)

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

So the function push does the wrong thing.

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.


Post a Comment for "Why Do I Get Only One Element Array By $('a') With JQuery Instead Of All Of Them?"