开发者

strange syntax in javascript to access a specific id in the dom

开发者 https://www.devze.com 2023-04-07 17:06 出处:网络
I am currently working on an old project with ton of legacy code. A syntax 开发者_StackOverflow社区I have never met is used to access a specific id in the dom in javascript.

I am currently working on an old project with ton of legacy code. A syntax 开发者_StackOverflow社区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?


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


$ 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消