[英]Select all 'a' elements from a page?
I want to select a link with some text
then I put: 我想选择一个带有some text
的链接,然后输入:
$("a[text='some text']")
But didn't work, then I want to test selecting all links from a page. 但是没有用,那么我想测试选择页面中的所有链接。
$("a")
But this jquery select instruction only gave me the first or maybe the most important link of the page. 但是,此jquery select指令仅给了我页面的第一个或最重要的链接。
Why? 为什么?
An example is here: 一个例子在这里:
Thanks in advance. 提前致谢。
Coding Horror does not make use of jQuery: there is no reference to the library in the source. 编码恐怖没有使用jQuery:在源代码中没有对库的引用。 If you tried jQuery('a')
instead, you would receive an error stating that jQuery
is not defined or it is not a function. 如果改用jQuery('a')
,则会收到一条错误消息,指出未定义jQuery
或它不是函数。
The reason $('a')
works anyway, but only returns the first element, is because $
is defined within Chrome's developer console, but as an alias of document.querySelector
. $('a')
仍然可以工作,但仅返回第一个元素的原因是,因为$
是在Chrome的开发人员控制台中定义的,但是作为document.querySelector
的别名。 This native method only returns the first matching element if any, unlike document.querySelectorAll
which returns all matching elements. 此本机方法仅返回第一个匹配元素(如果有),与document.querySelectorAll
不同,后者返回所有匹配元素。
There is a different command aliased to document.querySelectorAll
and that is $$
. 有别名为不同的命令document.querySelectorAll
那就是$$
。 Calling either one will yield all (256) elements matching the selector string: 调用任何一个都会产生与选择器字符串匹配的所有(256)个元素:
> $$('a')
NodeList[256]
> document.querySelectorAll('a')
NodeList[256]
the page doesnt actually have jQuery on it. 该页面实际上没有jQuery。 Type $ or window.$ into the console. 在控制台中键入$或window。$。 by typing $('a') you are using the chrome Command Line API. 通过输入$('a'),您正在使用chrome命令行API。 https://developers.google.com/chrome-developer-tools/docs/commandline-api https://developers.google.com/chrome-developer-tools/docs/commandline-api
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.