使用 Firefox 或 Chrome 的开发者控制台,当我输入 $('a')
时,它应该以数组形式返回页面上的所有链接(如果我选择了错误的框架,则不会例如,但这不是现在的问题)。请有人启发我,为什么我在某些网站上使用此 CSS 选择器在 Firefox 和 Chrome 上得到单个 a
结果,而在其他网站上得到一组 a
结果有多个 a
标签)?
这以前有效,我不记得它曾经像这样工作过。作为引用,我们以 http://www.useragentstring.com/pages/useragentstring.php 为例。
如果我在 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 thedocument.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/