javascript 中访问 dom 中特定 id 的奇怪语法

发布于 2024-12-05 23:01:50 字数 295 浏览 1 评论 0原文

我目前正在开发一个包含大量遗留代码的旧项目。 我从未见过的语法用于在javascript中访问dom中的特定id。

使用 $('btnsubmit'),而不是使用 document.getElementById("btnsubmit")

我从来没有遇到过这种语法。此外,Firebug 似乎也不喜欢它,因为它似乎会破坏调试器。我遇到的问题是,尽管该代码在生产站点上使用并且似乎可以工作,但代码似乎无法在调试环境中执行。

有人有关于这种语法的参考吗?它来自哪里,是否已被弃用?

I am currently working on an old project with ton of legacy code.
A syntax I have never met is used to access a specific id in the dom in javascript.

Instead of using document.getElementById("btnsubmit"), $('btnsubmit') is used.

I have never met this syntax. Moreover it seems that firebug doesn't like it either as it seems to break the debugger. I have issues where the code doesn't seem to be executed in a debugging environment although this code is used on a production site and seems to work.

Does any one have a reference on this syntax? Where does it comes from, is it deprecated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-12-12 23:01:50

它来自 javascript 库,一般来说它比 getElementById 更现代。不过,您需要包含脚本。

您的示例看起来像 Prototype

It's from a javascript library, and in general it's more modern than getElementById. You need the script include though.

Your example looks like Prototype

ゃ人海孤独症 2024-12-12 23:01:50

$ 只是 javascript 中的常规字符,它经常被 javascript 库使用并定义为函数名称,因此 $() 只是一个函数调用。在某些情况下,$ 可能被定义为 document.getElementById() 的同义词,作为节省打字的简写,而在其他情况下,它是一个更强大的 CSS3 样式选择器引擎(如在 jQuery 库中)。

在任何一种情况下,如果它在您的代码中未定义,那么您可能缺少代码所依赖的库引用。您需要找出您的代码编写时使用的库,并确保该库包含在该位置之前的代码中,以便正确定义 $() 函数。

$ is just a regular character in javascript and it is often used by javascript libraries and defined to be a function name so that $() is just a function call. In some cases, $ might be defined to be a synonym for document.getElementById() as shorthand to save typing and in other cases, it's a more robust CSS3 style selector engine (as in the jQuery library).

In either case, if its undefined in your code, then you are probably missing a library reference that your code relies on. You will need to find out what library your code was written to use and make sure that library is included in the code before this spot so that the $() function is defined properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文