<!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);
myParent3.adopt([myFirstElement,new Element('font#another')]);
結果:
<div id="parent">
<p id="second">
<ul id="third"></ul>
<a id="fourth"></a>
</p>
<font id="another"></font>
</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還有很多函數,大家如果感興趣可以看一下那個我在第一課時為大家提供下載的素材文件,里邊同時列出了其他一些不常用的函數.
(文章來源:http://see7di.cnblogs.com)
下一篇 Access數據庫技術(01)