$(".VoteButton--up.is-active");只返回了一個元素,而不是一個數組
發現頁面https://www.zhihu.com/follow裏面是無法訪問jQuery的,不清楚知乎這個頁面的$是怎麼定義的
function removeAllVotes(){ var a = $(".VoteButton--up.is-active"); var b = a.length; if (b > 0) { $('.VoteButton--up.is-active').first().trigger('click'); setTimeout(removeVote, 1000); window.scrollTo(0,document.body.scrollHeight); } } removeAllVotes();
不過document的api還是生效的,直接使用document.getElementsByClassName("VoteButton--up"),還是可以訪問到多個元素
Simple jQuery selector only selects first element in Chrome..?
I'm a bit new to jQuery so forgive me for being dense. I want to select all <td>
elements on a particular page via Chrome's JS console:
$('td')
Yet when I do this, I get the following output:
<td>Apples</td>
Isn't jQuery supposed to return an array of elements with the <td>
tag? Why am I only seeing the first element that matches this criteria?
Here is the site in question: http://www.w3schools.com/html/html_tables.asp
EDIT: I'd like to add that when I type a jQuery function into Chrome console, I do NOT get a jQuery object back. I get a plain HTML element. Something must be wrong with the way my Chrome is set-up/configured.
回答
If jQuery isn't present on the webpage, and of course no other code assigns something to $
, Chrome's JS console assigns $
a shortcut to document.querySelector()
.
You can achieve what you want with $$()
, which is assigned by the console a shortcut to document.querySelectorAll()
.
To know if the page contains jQuery, you can execute jQuery
in the console. To know if jQuery is assigned to $
, you can execute $().jquery
which will return jQuery version if that's the case.
Also, there are browser addons to inject jQuery in every webpage.
在console裏面,嘗試輸入jQuery是無法識別的。可以用兩個$來表示篩選所有,$$(".VoteButton--up")