css - 开发者控制台的 CSS 选择器返回错误结果

标签 css google-chrome firefox css-selectors

使用 Firefox 或 Chrome 的开发者控制台,当我输入 $('a') 时,它应该以数组形式返回页面上的所有链接(如果我选择了错误的框架,则不会例如,但这不是现在的问题)。请有人启发我,为什么我在某些网站上使用此 CSS 选择器在 Firefox 和 Chrome 上得到单个 a 结果,而在其他网站上得到一组 a 结果有多个 a 标签)?

这以前有效,我不记得它曾经像这样工作过。作为引用,我们以 http://www.useragentstring.com/pages/useragentstring.php 为例。

我的 Firefox 控制台结果: enter image description here Chrome : enter image description here

如果我在 stackoverflow 上执行此操作,CSS 选择器也可以工作。 Xpath 适用于每个站点。

最佳答案

来自Chrome dev tools documentation :

$(selector) returns the reference to the first DOM element with the specified CSS selector. This function is an alias for the document.querySelector() function.

这样您就可以在 useragentstring.com 上获得预期的行为。

在 Stackoverflow 上,您会得到不同的行为,因为页面中已包含 jQuery,并且 $ 变量已被 the jQuery object 覆盖。其中(当作为函数调用时):

Accepts a string containing a CSS selector which is then used to match a set of elements.

关于css - 开发者控制台的 CSS 选择器返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908577/

相关文章:

javascript - 样式所见即所得编辑器

php - 放入正文

html - float 和填充 - 等高

javascript - 检测用户是否安装了 Chrome 扩展程序

jquery - chrome 移动浏览器中的水平页面滚动

javascript - Windows 8 上的 Firefox 报告为触摸设备

javascript - Modal Lightbox Gallery 和 Carousel Slider 不兼容?

javascript - 在 Google Chrome 扩展程序中需要外部 JavaScript

css - 为什么 Firefox 特定属性在 Firebug 中可见

css - Webkit 绝对定位按钮一直延伸