jquery selector只返回第一个元素 知乎取消点赞

$(".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")

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(279)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-01-21 RedGate support tickets
2019-01-21 Autofac Getting Started(默认的构造函数注入)
2019-01-21 Autofac Controlling Scope and Lifetime
2019-01-21 Autofac register and resolve
2019-01-21 What's the difference between UTF-8 and UTF-8 without BOM?
2016-01-21 Finalization
点击右上角即可分享
微信分享提示