区别就是

$和$$都是1个参数,

$适用于ID,或者ID代表的对象

$$适用于CSS选择器

$E和$ES,有2个参数,第二个参数是可选参数代表(filter,即某个ID范围里的元素)

$E('input[type=text]','myform');//ID为myform里的第一个文本框

$ES('input[type=text]','myform');//ID为myform里的所有text文本框

Understanding Mootools Selectors $, $$, $E and $ES

Tagged $$$dollarJavascriptMootools

While browsing the Mootools forums I came across an excellent post by Fløe Rasmus. He explains to a mootools newbie how to use the selector functions $ and $$. In this article I’ll explain how $, $$, $E and $ES work, and as a bonus I’ll list all the dollar functions in Mootools v1.11.

The $ (dollar) function

Most people think this is just a shortcut for document.getElementById(), but actually it’s not. The $() takes one argument. This argument is called mixed in php terms, because it can be a string, or a dom element. Whenever the argument is a string s, it returns the element with id s with all Element methods applied. When the argument is a dom element, it just applies all Element methods to the element, and returns it. Here’s how it works:

  1. var w3cEl = document.getElementById('myDiv'); // w3cEl => W3C dom element
  2. var mooEl = $('myDiv'); // mooEl => Mootools Element
  3. var mooEl2 = $(w3cRef); // mooEl2 => Mootools Element
  4. var mooEl3 = $(mooEl2); // mooEl3 => mooEl2

So the $ function applies the Element methods to it’s argument. When passing a Mootools Element to $ it ‘ll be detected and no methods are applied, because the argument already has those methods (see example above mooEl3).

The $$ (double dollar) function

The double dollar function is more like document.getElementsByTagName() on steroids, both will return an array with multiple elements.

  1. var w3cArr = document.getElementsByTagName('div'); // w3cArr => Array of W3C dom elements
  2. var mooArr = $$('div'); // mooArr => Array of Mootools Elements

But $$ can do more. It’ll accept one or more css selectors (or elements) and return an array of elements matching those selectors:

  1. $$('a.external'); // => Array of links with class 'external'
  2. $$('a[href=#]'); // => Array of links with href="#"
  3. $$('form input[disabled]'); // Array of input elements inside a form with a disabled attribute
  4. $$('form input[disabled=disabled]'); // Array of disabled input elements inside a form (valid XHTML)
  5. $$('div[class^=foo]'); // => Array of divs with classname starting with 'foo'
  6. $$('[class$=bar]'); // => Array of elements with classname ending with 'bar'
  7. $$('*[class$=bar]'); // => Returns the same as the previous selector

Want to know more about the css3 selectors? Read the W3C css3 selector specification. Keep in mind the Mootools $$ function doesn’t support all selectors.

The $E function

This function is very much like the $ function. The first argument of $E is a css selector string and it returns the first found element found with the selector. The second element is the filter (id string or DOM element). See it like a scope, when the filter is passed, the selector is executed inside the filter element. So instead of passing an id to the dollar function, you can pass a css selector to $E.

  1. $E('div#foo'); // => Same as $('foo')
  2. $E('ul#bar li'); // => The first list item of the unordered list with id 'bar'
  3. $E('form input[type=checkbox]'); // => First checkbox of the first form
  4. $E('input[type=checkbox]', 'myForm'); // => First checkbox of the element with id 'myForm'

The $ES (Element.getElements) function

The $ES function is very much the same as the $$ and the $E functions. The first argument is the css selector, and the second argument is the filter. $ES returns an array of elements found with the css selector, which is executed inside the filter element. It’s pretty straightforward:

  1. $ES('a.external'); // => Same as $$('a.external')
  2. $ES('a.external', document); // => Same as $ES('a.external')
  3. $ES('input', 'myForm'); // => Array of input elements within the element with id 'myForm'
  4. $ES('input[type=text]', 'myForm'); // => Array of textbox elements within the element with id 'myForm'

Bonus: other $ functions

There are some more dollar function that come with Mootools:

  • $chk: Returns true if the passed in value/object exists or is 0, otherwise returns false.
  • $clear: Clears a timeout or an Interval.
  • $defined: Returns true if the passed in value/object is defined, that means is not null or undefined.
  • $extend: Copies all the properties from the second passed object to the first passed Object.
  • $merge: Merges a number of objects recursively without referencing them or their sub-objects.
  • $native: Will add a .extend method to the objects passed as a parameter, but the property passed in will be copied to the object’s prototype only if non previously existent.
  • $pick: Returns the first object if defined, otherwise returns the second.
  • $random: Returns a random integer number between the two passed in values.
  • $time: Returns the current timestamp.
  • $type: Returns the type of object that matches the element passed in.
  • $A: Returns a copy of the passed Array.
  • $each: Use to iterate through iterables that are not regular arrays, such as builtin getElementsByTagName calls, arguments of a function, or an object.
  • $H: Shortcut to create a Hash from an Object.
  • $RGB: Shortcut to create a new color, based on red, green, blue values.
  • $HSV: Shortcut to create a new color, based on hue, saturation, brightness values.
 

随机推荐

  1. 《MORE EFFECTIVE C++》条款27 要求或者禁止对象分配在堆上

    1. 要求对象分配在堆上 临时对象一般是存在于栈中的,或者是静态对象存在于常量存储区的.那么当创建一个这样的对象的时候,一般是需要隐式或显式地调用构造函数,在销毁的时候调用析构函数的.可以从这方面入手 ...

  2. CentOS下启动Tomcat

     http://tianlihu.iteye.com/blog/2010028 1. Tomcat的部署包存放的路径 /var/lib/tomcat6/webapps/ 2. Tomcat的配置文件路 ...

  3. java zip文件的解压缩(支持中文文件名)

    用的apache的ant包,下载导入即可.由于过程比较简单,直接上代码. 代码可直接复制使用. 如果想在android上使用,记得要在AndroidManifest.xml里添加权限: <use ...

  4. Install Fastx (zz)

    Fastx-toolkit installation on CentOS ===================================== Tested on CentOS release ...

  5. C++ 中的C_str()函数用法

    语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过 ...

  6. [转载] C++位运算:将一个4字节整数的二进制表示中的001替换为011

    #include <iostream> #include <assert.h> /** * Key: * * get someone bit: num & (mode1 ...

  7. 350. Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  8. 不容易系列之(3)—— LELE的RPG难题

    有排成一行的n个方格,用红(Red).粉(Pink).绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法. 思路:运用递归算法. a[1 ...

  9. scala言语基础学习十

    类型参数 泛型函数 多个参数 使用泛型参数时候,不给类型scala也能自己判断 上边界bounds 下边界bounds 专门用于打包泛型数组

  10. 学习笔记之 初试Linux遇到的问题 2015-10-13

    1. 安装.deb文件,用sudo gdebi XXX.deb sudo apt-get install xxx 2. 需要配置系统路径: LD_LIBRARY_PATH=.../lib:LD_LIB ...