温故:

  透过对上一节课的学习,相信大家对mt的选择器应该有了一定的认识了,我再放几个小示例让大家对选择器的复杂应用有所了解:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script style="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.4.1/mootools-yui-compressed.js"></script>
</head> <body id='a'>
<h2 class='a'>Single images</h2>
<p>
<a class='b' title="T1" href="http://www.digitalia.be/images/25.jpg"><img src="img/map1.png"></a>
<a class='c' title="B1" href="http://www.digitalia.be/images/24.jpg"><img src="img/map2.png"></a>
<a class='d' href="#"><img src="img/map2.png"></a>
</p>
<script type='text/javascript'>
alert($$('*').get('html'));//显示当前文件的html源码
alert($$('.b','.c').get('title'));//同时选择多个节点
alert($$('a[title=B1]').get('href'));//title='B1'的a节点
alert($$('[href^=http]').get('href'));//href以http开头的节点
alert($$('p > a').get('href'));//p下的a节点
alert($$('a:not(.c)').get('href'));//class不等于c的a节点
alert($$('a:index(0)').get('href'));//索引是0的a节点
alert($$("a[title][href$=jpg]").get('href'));//包含title属性,且href属性以jpg三个字母结尾的a节点
</script>
</body>
</html>

好了,我们今天的课程是函数,在今天的课程中我会把mt常常用到的一些函数讲解给大家:

set,setProperty:用来赋值,看例子:

$('a').set('value','123');
$('a').setProperty('class','abc');

get,getProperty:用来取值,看例子:

$('a').get('value');
$('a').getProperty('class');

hasClass,addClass,removeClass,toggleClass://判断是否有某样式,新增样式,移除样式,交换样式,看例子

alert($('a').hasClass('abc'));
$('a').addClass('abc');
$('a').removeClass('abc');
$('a').toggleClass:('bc');

setStyle,getStyle://设置或获取css样式,看例子

$('a').setStyle('display','none');
alert($('a').getStyle('display'));

getSize://得到宽度和高度,看例子

var size=$('a').getSize();
alert(size.x+'|'+size.y);

getPosition,setPosition://返回或设置偏移值,看例子

$('a').getPosition();//returns{x:100,y:500};
$('a').setPosition({x:10,y:100});

destroy://删除元素自身及所有子节点,然后内存清理

$('div').destroy();

store,retrieve://向元素存储区存放或读取值(与jq的data类似)

$('a').store('someProperty',someValue);
$('a').retrieve('someProperty');//returns someValue

inject://向元素指定位置插入

 _cut:function(el){//把Element剪切并粘贴到el内部所有内容之前,父子
return this.inject($(el),'top');//$('t1')._cut($('t3'));
},
cut_:function(el){//把Element剪切并粘贴到el内部所有内容之后,父子
return this.inject($(el));//$('t1').cut_($('t3'));
},
_move:function(el){//把el平移到Element之前,兄弟
return el.inject(this,'before');//$('t1')._move($('t3'));
},
move_:function(el){//把el平移到Element之后,兄弟
return el.inject(this,'after');//$('t1')._move($('t3'));
},
_xmove:function(el){//把Element平移到el之前,兄弟
return this.inject($(el),'before');//$('t1')._xmove($('t3'));
},
xmove_:function(el){//把Element平移到el之后,兄弟
return this.inject($(el),'after');//$('t1').xmove_($('t3'));
},

adopt://向元素内部插入子元素

示例:
var myFirstElement =new Element('div#first');
var mySecondElement=new Element('p#second');
var myThirdElement =new Element('ul#third');
var myFourthElement=new Element('a#fourth');
var myParentElement=new Element('div#parent');
myFirstElement.adopt(mySecondElement);
mySecondElement.adopt('third',myFourthElement);
myThirdElement.adopt([myFirstElement,new Element('span#another')]); 结果:
<div id="parent">
<p id="second">
<ul id="third"></ul>
<a id="fourth"></a>
</p>
<span id="another"></span>
</div>

typeOf://返回类型
    返回的类型:
    'element' - (string) 单个节点
    'elements' - (string) 多个节点
    'textnode' - (string) 文本节点
    'whitespace' - (string) If object is a DOM whitespace node.
    'arguments' - (string) If object is an arguments object.
    'array' - (string) If object is an array.
    'object' - (string) If object is an object.
    'string' - (string) If object is a string.
    'number' - (string) If object is a number.
    'date' - (string) If object is a date.
    'boolean' - (string) If object is a boolean.
    'function' - (string) If object is a function.
    'regexp' - (string) If object is a regular expression.
    'class' - (string) If object is a Class (created with new Class or the extend of another class).
    'collection' - (string) If object is a native HTML elements collection,such as childNodes or getElementsByTagName.
    'window' - (string) If object is the window object.
    'document' - (string) If object is the document object.
    'domevent' - (string) If object is an event.
    'null' - (string) If object is undefined,null,NaN or none of the above.

    示例:
var myString='hello';
alert(typeOf(myString));

attempt://类似try

    Function.attempt(
function(){
alert('a');
},
function(){
alert('b');
},
function(){
alert('c');
}
);

delay://延时执行

    function LoadCook(){
clearTimeout(timer);
alert('a');
}var timer=LoadCook.delay(2000);

trim://去除两端空格

    alert(' 啊 '.trim());

toInt,toFloat://转为整数,转为小数

    '4em'.toInt();//returns 4
'10px'.toInt();//returns 10
'95.25%'.toFloat();//returns 95.25
'10.848'.toFloat();//returns 10.848

toLowerCase,toUpperCase://转为小写,转为大写

    'AFD'.toLowerCase();
'ffdsa'.toUpperCase();

延伸:
  我上边所讲解的这些函数都是我们在日常开发中最常常用到的一些,当然了mt还有很多函数,大家如果感兴趣可以看一下那个我在第一课时为大家提供下载的素材文件,里边同时列出了其他一些不常用的函数.

一周学会Mootools 1.4中文教程:(2)函数的更多相关文章

  1. 一周学会Mootools 1.4中文教程:(7)汇总收尾

    转眼之间已经第七课了,这也将成为最后一课,如果这7课下来您感觉水平没有达到预想的水平,没关系您可以继续关注本站的博文,我会陆续发一些类似的文章帮您提升水平,另外我最近打算和群里的几个Mootools爱 ...

  2. 一周学会Mootools 1.4中文教程:序论

    刚才发了几篇Mootools(以后直接简称Moo或Mt,看到这两个名字的时候不要感到奇怪),有一位热心的朋友"追杀"告诉我说现在已经出到1.4了,就不要再纠结于1.2了,想象一下有 ...

  3. 一周学会Mootools 1.4中文教程:(1)Dom选择器

    利器: 君欲善其事须先利其器,好吧因为我们的时间比较紧迫,只有六天而已,那么六天的时间用死记硬背的方式学会Mt犹如天方夜谭,因此我们需要借鉴一下Editplus的素材栏帮我们记忆就好了,当我们需要用到 ...

  4. 一周学会Mootools 1.4中文教程:(6)动画

    先看一下动画的参数设置: 参数: fps - (number:默认是50) 每秒的帧数. unit - (string:默认是 false) 单位,可为 'px','em',或 '%'. link - ...

  5. 一周学会Mootools 1.4中文教程:(5)Ajax

    ajax在我们前台的开发中是非常重要的,所以我们单独拿出一节课来讲述,首先我们看一下mootools的ajax构成 语法: var myRequest=new Request([参数]); 参数: u ...

  6. 一周学会Mootools 1.4中文教程:(3)事件

    今天我們講解一下mt的事件部分,对于事件的讲解主要包含三部分,分别是:绑定,移除,和触发,我们首先来看一个例子 //jquery的事件绑定方式$('a').click(function){ alert ...

  7. 一周学会Mootools 1.4中文教程:(4)类型

    Mootools的类型主要包含下边几部分:String:字符串;Number:数字;Array:数组;Object:对象;Json:;Cookie:. 这也是我们今天的讲述重点.每一种数据类型Mt都为 ...

  8. Swift中文教程(四)--函数与闭包

    原文:Swift中文教程(四)--函数与闭包 Function 函数 Swift使用func关键字来声明变量,函数通过函数名加小括号内的参数列表来调用.使用->来区分参数名和返回值的类型: fu ...

  9. 一周学会go语言并应用 by王奇疏

    <一周学会go语言并应用> by王奇疏 ( 欢迎加入go语言群: 218160862 , 群内有实践) 点击加入 零.安装go语言,配置环境及IDE 这部分内容不多,请参考我的这篇安装环境 ...

随机推荐

  1. oracle中文支持

  2. Minimum Inversion Number(归并排序)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  3. Android中的数据存储

    Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...

  4. 按模板打印word防止并发操作

    /// <summary> /// /// <summary> /// 打印人员备案表 /// </summary> /// <param name=&quo ...

  5. win7环境下安装MongoDB

    1.从http://www.mongodb.org/downloads获取,下载适合windows版本的mongodb,注意32位和64位的区别2.将下载的zip版本,解压到D:/mongodb3.创 ...

  6. jQuery的三种$()

    参考脚本之家“http://www.jb51.net/article/21660.htm”   $号是jQuery“类”的一个别称,$()构造了一个jQuery对象.所以,“$()”可以叫做jQuer ...

  7. osg项目经验1<MFC+OSG中模型点选效果>

    点选主要是重载osg的GUIEventHandler, class CPickHandler : public osgGA::GUIEventHandler{ //自定义回调函数名:CPickHand ...

  8. leetcode find median sorted arrays python

    # @link http://www.cnblogs.com/zuoyuan/p/3759682.html class Solution(object): def findMedianSortedAr ...

  9. apache hide index.php

    <Directory "D:/usr/local/www">    AllowOverride all    Options +FollowSymLinks +SymL ...

  10. SQL2-子查询、join查询

    SQL常用高级查询包括:Join查询.子查询. 子查询: USE flowershopdb --子查询:在一个select语句使用另一个select 语句作为条件或数据来源. --查询块:一个sele ...