简单的 jQuery 选择器只选择 Chrome 中的第一个元素..? [英] Simple jQuery selector only selects first element in Chrome..?

查看:35
本文介绍了简单的 jQuery 选择器只选择 Chrome 中的第一个元素..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 jQuery 有点陌生,所以请原谅我过于密集.我想通过 Chrome 的 JS 控制台选择特定页面上的所有 元素:

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>

jQuery 不是应该返回带有 <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?

这是有问题的网站:http://www.w3schools.com/html/html_tables.asp

我想补充一点,当我在 Chrome 控制台中输入一个 jQuery 函数时,我没有得到一个 jQuery 对象.我得到一个普通的 HTML 元素.我的 Chrome 的设置/配置方式一定有问题.

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.

推荐答案

如果网页上没有 jQuery,当然也没有其他代码给 $ 赋值,Chrome 的 JS 控制台会分配 $ document.querySelector() 的快捷方式.

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

您可以使用 $$() 实现您想要的功能,它由控制台分配给 document.querySelectorAll() 的快捷方式.

You can achieve what you want with $$(), which is assigned by the console a shortcut to document.querySelectorAll().

要知道页面是否包含jQuery,可以在控制台中执行jQuery.要知道 jQuery 是否分配给 $,您可以执行 $().jquery 如果是这种情况,它将返回 jQuery 版本.

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.

此外,还有浏览器插件可以在每个网页中注入 jQuery.

Also, there are browser addons to inject jQuery in every webpage.

这篇关于简单的 jQuery 选择器只选择 Chrome 中的第一个元素..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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