并不是js的什么特殊用法。其实只是你的页面引用到的js(或被include页面的js)文件中定义有 名为$的这样一个function而已。
就我本人来说,开始在一个页面中看到这样一种用法:
后来终于在这个页面include的另一个页面包含的prototype.js中发现定义有这样一个名为$的function。
附prototype.js中 $() 的定义:
参考:
[url]http://www.javascriptsearch.com/guides/Advanced/articles/0607ABetterDollarFunction.html[/url]
[url]http://osric.com/chris/accidental-developer/2008/04/the-javascript-dollar-sign-function/[/url]
[url]http://www.phpguru.org/article/the-javascript-dollar-function[/url]
[url]http://javascript.internet.com/snippets/prototype-dollar-function.html[/url]
[b]The dollar function must die[/b]
[url]http://blog.dorward.me.uk/2009/02/19/the-dollar-function-must-die.html[/url]
[quote]
A number of JavaScript libraries have a function called $ and I’m irritated by it for a number of reasons:
The dollar sign is intended for use only in mechanically generated code.
… which the libraries are not.
The name doesn’t give a clue as to what the function does or where maintainers should look for documentation.
Different libraries use the $ function to do different things. In Prototype.js it means “Get the element with this id”, but in jQuery is means “Get all the elements that match this CSS selector”. Maintainers have to know what library is in use to know what the function does and can’t easily tell by just looking at the code.
If you have a library with a $ function, please don’t use it. Good libraries have alternatives (such as jQuery) or don’t create the function at all (such as YUI).
[/quote]
就我本人来说,开始在一个页面中看到这样一种用法:
后来终于在这个页面include的另一个页面包含的prototype.js中发现定义有这样一个名为$的function。
附prototype.js中 $() 的定义:
function $(element) {
if (arguments.length > 1) {
for (var i = 0, elements = [], length = arguments.length; i < length; i++)
elements.push($(arguments[i]));
return elements;
}
if (typeof element == 'string')
element = document.getElementById(element);
return Element.extend(element);
}
参考:
[url]http://www.javascriptsearch.com/guides/Advanced/articles/0607ABetterDollarFunction.html[/url]
[url]http://osric.com/chris/accidental-developer/2008/04/the-javascript-dollar-sign-function/[/url]
[url]http://www.phpguru.org/article/the-javascript-dollar-function[/url]
[url]http://javascript.internet.com/snippets/prototype-dollar-function.html[/url]
[b]The dollar function must die[/b]
[url]http://blog.dorward.me.uk/2009/02/19/the-dollar-function-must-die.html[/url]
[quote]
A number of JavaScript libraries have a function called $ and I’m irritated by it for a number of reasons:
The dollar sign is intended for use only in mechanically generated code.
… which the libraries are not.
The name doesn’t give a clue as to what the function does or where maintainers should look for documentation.
Different libraries use the $ function to do different things. In Prototype.js it means “Get the element with this id”, but in jQuery is means “Get all the elements that match this CSS selector”. Maintainers have to know what library is in use to know what the function does and can’t easily tell by just looking at the code.
If you have a library with a $ function, please don’t use it. Good libraries have alternatives (such as jQuery) or don’t create the function at all (such as YUI).
[/quote]