<!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'));//顯示當(dāng)前文件的html源碼
alert($('.b','.c').get('title'));//同時(shí)選擇多個(gè)節(jié)點(diǎn)
alert($('a[title=B1]').get('href'));//title='B1'的a節(jié)點(diǎn)
alert($('[href^=http]').get('href'));//href以http開頭的節(jié)點(diǎn)
alert($('p > a').get('href'));//p下的a節(jié)點(diǎn)
alert($('a:not(.c)').get('href'));//class不等于c的a節(jié)點(diǎn)
alert($('a:index(0)').get('href'));//索引是0的a節(jié)點(diǎn)
alert($("a[title][href$=jpg]").get('href'));//包含title屬性,且href屬性以jpg三個(gè)字母結(jié)尾的a節(jié)點(diǎn)
</script>
</body>
</html>
好了,我們今天的課程是函數(shù),在今天的課程中我會(huì)把mt常常用到的一些函數(shù)講解給大家:
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://設(shè)置或獲取css樣式,看例子
$('a').setStyle('display','none');
alert($('a').getStyle:('display'));
getSize://得到寬度和高度,看例子
var size=$('a').getSize();
alert(size.x+'|'+size.y);
getPosition,setPosition://返回或設(shè)置偏移值,看例子
$('a').getPosition();//returns{x:100,y:500};
$('a').setPosition({x:10,y:100});
destroy://刪除元素自身及所有子節(jié)點(diǎn),然后內(nèi)存清理
$('div').destroy();
store,retrieve://向元素存儲(chǔ)區(qū)存放或讀取值(與jq的data類似)
$('a').store('someProperty',someValue);
$('a').retrieve('someProperty');//returns someValue
inject://向元素指定位置插入
_cut:function(el){//把Element剪切并粘貼到el內(nèi)部所有內(nèi)容之前,父子
return this.inject($(el),'top');//$('t1')._cut($('t3'));
},
cut_:function(el){//把Element剪切并粘貼到el內(nèi)部所有內(nèi)容之后,父子
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://向元素內(nèi)部插入子元素
示例:
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);
myParent3.adopt([myFirstElement,new Element('font#another')]);
結(jié)果:
<div id="parent">
<p id="second">
<ul id="third"></ul>
<a id="fourth"></a>
</p>
<font id="another"></font>
</div>
typeOf://返回類型
返回的類型:
'element' - (string) 單個(gè)節(jié)點(diǎn)
'elements' - (string) 多個(gè)節(jié)點(diǎn)
'textnode' - (string) 文本節(jié)點(diǎn)
'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://延時(shí)執(zhí)行
function LoadCook(){
clearTimeout(timer);
alert('a');
}var timer=LoadCook.delay(2000);
trim://去除兩端空格
alert(' 啊 '.trim());
toInt,toFloat://轉(zhuǎn)為整數(shù),轉(zhuǎn)為小數(shù)
'4em'.toInt();//returns 4
'10px'.toInt();//returns 10
'95.25%'.toFloat();//returns 95.25
'10.848'.toFloat();//returns 10.848
toLowerCase,toUpperCase://轉(zhuǎn)為小寫,轉(zhuǎn)為大寫
'AFD'.toLowerCase();
'ffdsa'.toUpperCase();
延伸:
我上邊所講解的這些函數(shù)都是我們?cè)谌粘i_發(fā)中最常常用到的一些,當(dāng)然了mt還有很多函數(shù),大家如果感興趣可以看一下那個(gè)我在第一課時(shí)為大家提供下載的素材文件,里邊同時(shí)列出了其他一些不常用的函數(shù).
(文章來源:http://see7di.cnblogs.com)