从页面中选择所有“a”元素? [英] Select all 'a' elements from a page?

查看:206
本文介绍了从页面中选择所有“a”元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择一个链接与一些文本然后我把:

I want to select a link with some text then I put:

$("a[text='some text']")

但没有工作,然后我想测试从页面中选择所有链接。

But didn't work, then I want to test selecting all links from a page.

$("a")

但是这个jQuery选择指令只给了我第一个也可能是页面最重要的链接。

But this jquery select instruction only gave me the first or maybe the most important link of the page.

为什么?

一个例子是这里:

An example is here:

提前感谢

推荐答案

编码恐怖没有使用jQuery:没有引用源中的库。如果您尝试使用 jQuery('a'),则会收到一条错误,指出 jQuery 未定义不是一个功能。

Coding Horror does not make use of jQuery: there is no reference to the library in the source. If you tried jQuery('a') instead, you would receive an error stating that jQuery is not defined or it is not a function.

原因 $('a')无论如何,只能返回第一个元素是因为 $ 在Chrome的开发者控制台中定义,而是以 document.querySelector 的别名定义。这个本机方法只返回第一个匹配的元素,如果有的话,与 document.querySelectorAll 不同,返回所有匹配元素。

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. This native method only returns the first matching element if any, unlike document.querySelectorAll which returns all matching elements.

调用任一个将产生与选择器字符串匹配的所有(256)个元素:

There is a different command aliased to document.querySelectorAll and that is $$. Calling either one will yield all (256) elements matching the selector string:

> $$('a')
NodeList[256]

> document.querySelectorAll('a')
NodeList[256]

$ $$ 记录在案 here

这篇关于从页面中选择所有“a”元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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