Greasy Fork is available in English.

討論 » 開發

[SOLVED] Problems with getting text in class

§
發表於:2016-12-29
編輯:2016-12-29

[SOLVED] Problems with getting text in class

Hi, I'm trying to write a script to extract the comments from 9gag.com/gag/* pages. (e.g. link ). I'm using the code below. I tried solving this with jQuery but this failed as well.

(function() { 'use strict'; var elements = document.getElementsByClassName('content badge-content'); console.log(elements); console.log(elements.length); for (var i = 0; i < elements.length; i++) { console.log(i); var text = elements[i].innerText; console.log(text); } })();

I attached Chromes console output below... Whereas elements seems to be 19 elements long, elements.length gives 0. So my for loop is not running...

Do you have any ideas??? Thanks for any help ;)

EDIT: Adding an Event listener solved the problem.

window.addEventListener("load", function() {

}
§
發表於:2016-12-29

Oh and element[0].innerText failed with the error: cannot get innerText from undefined.

發表回覆

登入以回復