在某些网站上使用 jQuery 函数会导致 Uncaught TypeError

Using jQuery functions on some websites cause Uncaught TypeError

我正在通过在 Chrome 的控制台中编写一些代码来学习 jQuery,例如将 id 添加到 DOM 元素,然后执行 jQuery 函数。

在某些网站上(例如 Google),我得到 Uncaught TypeError,而在其他网站上,我没有。这是一个例子:

DOM修改:

<input id="test" aria-label="Szukaj w Google" name="btnK" type="submit" jsaction="sf.chk">

控制台:

$("#test").hide()

错误:

VM598:1 Uncaught TypeError: $(...).hide is not a function
at <anonymous>:1:12

其他网站(例如 Whosebug)上的相同流程有效。这是什么原因?

jQuery 未在 Internet 上的许多网站上加载。这就是您收到错误消息 Uncaught TypeError: $(...).hide is not a function 的原因,因为实际上 $ 不是 jQuery,而是另一个函数、对象,或者只是未定义。

尽管您可以在 $ 符号下找到 one of many versions of jQuery loaded on numerous websites, several companies/projects/websites don't use jQuery, or they use a custom build of it 或其他内容,或者根本找不到。


在网站上,默认

如果该特定网站未使用其他实现(例如自定义函数、库、jQuery 等)覆盖这些变量。 More on this

归功于@AuxTaco


如果您需要在网站上显示 jQuery(例如,用于开发目的),但如果它不存在,you can inject it into the website.