mootools里选择器$,$$,$E,$ES等的区别
区别就是
$和$$都是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 $, $$, dollar, Javascript, Mootools
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:
var w3cEl = document.getElementById('myDiv'); // w3cEl => W3C dom element
var mooEl = $('myDiv'); // mooEl => Mootools Element
var mooEl2 = $(w3cRef); // mooEl2 => Mootools Element
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.
var w3cArr = document.getElementsByTagName('div'); // w3cArr => Array of W3C dom elements
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:
$$('a.external'); // => Array of links with class 'external'
$$('a[href=#]'); // => Array of links with href="#"
$$('form input[disabled]'); // Array of input elements inside a form with a disabled attribute
$$('form input[disabled=disabled]'); // Array of disabled input elements inside a form (valid XHTML)
$$('div[class^=foo]'); // => Array of divs with classname starting with 'foo'
$$('[class$=bar]'); // => Array of elements with classname ending with 'bar'
$$('*[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.
$E('div#foo'); // => Same as $('foo')
$E('ul#bar li'); // => The first list item of the unordered list with id 'bar'
$E('form input[type=checkbox]'); // => First checkbox of the first form
$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:
$ES('a.external'); // => Same as $$('a.external')
$ES('a.external', document); // => Same as $ES('a.external')
$ES('input', 'myForm'); // => Array of input elements within the element with id 'myForm'
$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.
随机推荐
- 作业6 分析项目的NABCD和项目的产品Backlog
项目scrum:邵家文 NABCD模型分析 N(Need 需求)根据采访用户下面可以得出用户的基本需求:1.小孩说:我想要做适合自己能力的四则运算2.小孩说:我想这个四则运算软件里面的题目越做越提高自 ...
- SecureCRT相关
-------------------------------------------------- 如何解决SecureCRT汉字乱码的问题? 选择工具栏上的“选项”菜单在打开的下拉菜单中选择“会话 ...
- Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- eclipse 中文乱码
使用Eclipse编辑文件经常出现中文乱码或者文件中有中文不能保存的问题,Eclipse提供了灵活的设置文件编码格式的选项,我们可以通过设置编码 格式解决乱码问题.在Eclipse可以从几个层面设置编 ...
- [转载]oracle 11g不能导出空表的多种解决方法
原文地址:11g不能导出空表的多种解决方法">oracle 11g不能导出空表的多种解决方法作者:Anlin_Yang ORACLE 11g 用exp命令导出库文件备份时,发现只能导出 ...
- CSS基础知识点(一)
CSS(Cascading Style Sheets)全称为:层叠样式表. 1.HTML元素类型 (1) 内联元素(inline):可以理解为“文本模式”,即从左到右顺序显示,不单独换行.常用的内联元 ...
- mysql导入sql文件过大或连接超时的解决办法
前段时间出差在现场开发的时候,导入数据库老是出问题.最后发现了一个神奇sql语句交给实施,只要导入出错就把如下语句执行就可以了.至今屡试不爽. 1 2 3 4 5 6 7 set global max ...
- ubuntu12.04 修复Grub2
电脑双系统,但是把win7重装了之后,会发现grub坏了,只能进入win7. 遇到过好几次,虽然每次都成功解决问题了,但是都花费了不少时间. 所以,总结一下,基本是从网上找到的方法,有的行不通,有的可 ...
- Tomcat的JVM优化
一.JVM管理内存段分类 1.线程共享内存 方法区:存储jvm加载的class.常量.静态变量.及时编译器编译后的代码等 java堆:存储java所有对象实例.数组等 2.线程私有内存 程序计数寄存器 ...
- URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)
Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construc ...