第一章:入門(mén)
$()取得頁(yè)面中的元素
$(document).ready(function(){
$('.poem-stanza').addClass('emphasized');
});
說(shuō)明:
$('.poem-stanza') //取得頁(yè)面中添加了類(lèi)poem-stanza的元素
.addClass('emphasized'); //添加Css樣式,emphasized為類(lèi)樣式
.removeClass();
第二章:選擇符
$(document).ready(function(){
//CSS選擇符
$('#selectedplays>li').addClass('horizontal'); //查找id為selectedplays元素的頂層子元素li,添加樣式
$('#selectedplays>li:not(.horizotal)').addClass('sublevel'); //查找id為selectedplays元素的頂層子元素li,并且沒(méi)有類(lèi)horizotal
//XPath選擇符
$('a[@href^="mailto:"]').addClass('mailto'); //查找錨元素中屬性href以“mailto:”開(kāi)頭的元素
$('a[@href$=".pdf"]').addClass('pdflink'); //查找錨元素中屬性href以“.pdf”結(jié)尾的元素
$('a[@href*="mysite.com"]').addClass('mysite'); //查找錨元素中屬性href中包含“mysite.com”的元素(任意位置)
//自定義選擇符
$('tr:odd').addClass('odd'); //查找奇數(shù)行
$('tr:even').addClass('even'); //查找偶數(shù)行
//注::odd()和:even()選擇符使用的都是Javascript本身的從0開(kāi)始得編號(hào)方式。表格的第1行編號(hào)為0(偶數(shù)),而第2行編號(hào)為1(奇數(shù))
$('td:contains("Henry")').addClass('highlight'); //查找td中包含"Henry"的
//DOM遍歷方法
$('th').parent().addClass('tableheading'); //查找th的父元素即標(biāo)題行
$('tr:not([th]):even').addClass('even'); //查找tr元素中不包含子為th的,并且是偶數(shù)行
$('tr:not([th]):odd').addClass('odd');
$('td:contains("Henry")').next().addClass('highlight'); //查找td中包含“Henry”的單元格的下一個(gè)單元格
$('td:contains("Comedy")').siblings().addClass('highlight'); //查找td中包含“Comedy”的同級(jí)單元格
$('td:contains("Comedy")').parent().find('td:gt(0)').addClass('highlight');
//查找td中包含“Comedy”的父,再去查找td單元格編號(hào)大于0的單元格
$('td:contains("Comedy)').parent().find('td').not(':contains("Comedy")').addClass('highlight');
//查找td中包含“Comedy”的父,再去查找td單元格不包含“Comedy”的單元格
//訪問(wèn)DOM元素
var tag = $('#myelement').get(0).tagName;
});
第三章:事件――扣動(dòng)扳機(jī)
綁定事件:
.bind(‘click’,function(){})
$(‘#switcher-large’).bind(‘click’,function(){…});給id為switcher-large的元素綁定click事件
可簡(jiǎn)寫(xiě)為
$(‘#switcher-large’).click(function(){…});
方法:
.toggle(function(){},function(){})
接受兩個(gè)參數(shù)(都是函數(shù))第一次點(diǎn)擊調(diào)用第一個(gè)函數(shù),第二次點(diǎn)擊調(diào)用第二個(gè)
$(‘#switcher h3’).toggle(function(){
$(‘#switcher .button’).addClass(‘hidden’);
},function(){
$(‘#switcher .button’).removeClass(‘hidden’);
});
.toggleClass()是一種跟優(yōu)雅得方案,但對(duì)于交替執(zhí)行的操作而言,.toggle()的適用性更強(qiáng)
$(‘#switcher h3’).click(function(){
$(‘#switcher .button’).toggleClass(‘hidden’); //自動(dòng)檢查該類(lèi)是否存在
});
.hover(function(){},function(){})
接受兩個(gè)函數(shù)參數(shù),第一個(gè)指針進(jìn)入,第二個(gè)指針離開(kāi)
$(‘#switcher .button’).hover(function(){
$(this).addClass(‘hover’);
},function(){
$(this).removeClass(‘hover’);
});
.unbind(‘click’)
移除事件
.trigger(‘click’)
模擬用戶操作
$(‘#switcher’).trigger(‘click’); 模擬點(diǎn)擊了一次switcher
第四章:效果――為操作添加藝術(shù)性
1.修改內(nèi)聯(lián)CSS
語(yǔ)法:
.css(‘property’,’value’);
.css(‘property’:’value’,’property’:’value’);
用法:
var currentSize = $(‘div.speech’).css(‘fontSize’);//得到div.speech得字體大小
var num = parasFloat(currentSize,10); //將currentSize轉(zhuǎn)換為Float型
var unit = currentSize.slice(-2); //返回從指定的字符開(kāi)始的一個(gè)子字符串,-2為倒數(shù)第二個(gè)
num *= 1.5;
$(‘div.speech’).css(‘fontSize’,num+unit); //設(shè)置樣式
2.基本的顯示和隱藏
.show();
.hide();
用法:
$(‘p:eq(1)’).hide(); //將第二個(gè)段落隱藏
3.效果和速度
指定顯示速度
3種:slow、normal和fast 時(shí)間:0.6、0,4、0.2。也可以指定.show(850)
$(‘p:eq(2)’).show(‘slow’);
淡入和淡出
.fadeIn(); //淡出
.fadeOut(); //淡出
$(‘p:eq(2)’).fadeIn(‘slow’);
4.多重效果
語(yǔ)句:.animate({param1:’value1’,parame2:’value2’},speed,function(){回調(diào)});
用法:$(‘p:eq(1)’).animate({height:’show’,width:’show’,opacity:’show’},’slow’);
這里使用簡(jiǎn)寫(xiě)的show將高度、寬度回復(fù)到被隱藏之前的值
5.并發(fā)與排隊(duì)效果
處理一組元素
$(‘div.buttont’).animate({left:650},’slow’).animate({height:38},’slow’);
先移動(dòng)到left為650的位置,在將高度設(shè)置為38
$(‘div.button’)
.fadeTo(‘slow’,0.5) //先淡化
.animate({left:650},’slow’) //在移動(dòng)
.fadeTo(‘slow’,1.0) //在淡出
.slideUp(‘slow’) //最后滑到上方隱藏
注意:排隊(duì)不適用于其他的非效果方法,例如:.css()
處理多組元素
$(‘p:eq(2)’).slideUp(‘slow’).next().slideDown(‘slow’); 段落3向上滑出,同時(shí)段落4向下滑入
回調(diào)函數(shù):
Var $thisPara = $(‘p:eq(2)’);
$(‘p:eq(2)’).next().slideDown(‘slow’,function(){
$thisPara.slideUp(‘slow’);
});
段落4下滑完成后,段落3才開(kāi)始上滑
第五章:DOM操作-基于命令改變頁(yè)面
1.操作屬性
非css屬性
$(document).ready(function(){
$('div.chapter a[@href*=wikipedia]').each(function(index){
var $thisLink = $(this);
$thisLink.attr({
'rel': 'external',
'id': 'wikilink-' + index,
'title': 'learn more about ' + $thisLink.text() + ' at wikipedia'
});
});
});
//遍歷div.chapter 下面所以錨元素href中包含wikipedia,為其添加屬性
深入理解$()工廠函數(shù)
$('<a href="#top">back to top</a>'); 創(chuàng)建新元素
2.插入新元素
.insertBefore()
.before()
作用相同,區(qū)別取決于將它們與其他方法進(jìn)行連綴
.insertAfter()
.after()
// $('div.chapter p').after('<a href="#top">back to top</a>');
// $('div.chapter p').before('<a href="#top">back to top</a>');
$('<a href="#aaa">back to top</a>').insertAfter('div.chapter p');
$('<a id="aaa" name="top"></a>').prependTo('body');
.perpendTo插入了作為目標(biāo)的錨
// $('<a href="#top">back to top</a>').insertBefore('div.chapter p');
3.移動(dòng)元素
$('span.footnote').insertBefore('#footer');
將span中類(lèi)為footnote的插入到id為footer的前面
標(biāo)注、編號(hào)和鏈接到上下文
$('span.footnote').each(function(index){
$(this)
.before('<a href="#foot-note-' + (index+1) +
'" id="context-' + (index+1) +
'" class="context"><sup>' + (index+1) +
'</sup></a>');
});
$('span.footnote').insertBefore('#footer');
遍歷span.footnote,在各個(gè)元素前加標(biāo)注后,在將span.footnote添加到#footer前
插入腳注
$('<ol id="notes"></ol>').insertAfter('div.chapter');
$('span.footnote').each(function(index){
$(this)
.before('<a href="#foot-note-' + (index+1) +
'" id="context-' + (index+1) +
'" class="context"><sup>' + (index+1) +
'</sup></a>')
.appendTo('#notes')
.append(' (<a href="#context-'+(index+1)+'">context</a>)');
});
先創(chuàng)建一個(gè)ol元素notes,并添加到div.chapter后面,遍歷span.footnote,先添加標(biāo)注,然后通過(guò)appendTo其添加到ol末尾,最后又通過(guò)append添加一個(gè)錨元素。
4.包裝元素
.wrap()
.wrap('<li id="foot-note-' + (index+1) + '"></li>')
5.復(fù)制元素
.clone()
$('div.chapter p:eq(0)').clone().insertBefore('div.chapter');
將div.chapter中得第一段復(fù)制后插入在div.chapter前面
深度復(fù)制
$('div.chapter p:eq(0)').clone(false)
只復(fù)制結(jié)構(gòu),內(nèi)部的文本沒(méi)有復(fù)制過(guò)來(lái),因?yàn)槲谋疽彩荄OM節(jié)點(diǎn)
通過(guò)復(fù)制創(chuàng)建突出引用
var $parentParagraph = $(this).parent('p');
$parentParagraph.css('position','relative');
$(this).clone()
.addClass('pulled')
.prependTo($parentParagraph);
修飾突出引用
$clonedCopy.addClass('pulled')
.find('span.drop')
.html('…')
.end()
.prependTo($parentParagraph)
.wrap('<div class="pulled-wrapper"></div>');
var clonedText = $clonedCopy.text();
$clonedCopy.html(clonedText);
第六章:AJAX 讓網(wǎng)站與時(shí)俱進(jìn)
1.基于請(qǐng)求加載數(shù)據(jù)
追加HTML
//載入遠(yuǎn)程 HTML 文件代碼并插入至 DOM 中
$('#dictionary').load('a.html',function(){
Alert(“加載完畢!”);//回調(diào)函數(shù)
})
操作JavaScript對(duì)象
JSON:
[
{
"term": "BACKBITE",
"part": "v.t.",
"definition": "To speak of a man as you find him when he can't find you."
},
{
"term": "BEARD",
"part": "n.",
"definition": "The hair that is commonly cut off by those who justly execrate the absurd Chinese custom of shaving the head."
}
]
JQuery:
$.getJSON('b.json',function(data){ //通過(guò) HTTP GET 請(qǐng)求載入 JSON 數(shù)據(jù)
$.each(data,function(entryIndex,entry){
var html = '<div class="entry">';
html += '<h3 class="term">' + entry['term'] + '</h3>';
html += '<div class="pare">' + entry['part'] + '</div>';
html += '<div class="definition">';
html += entry['definition'];
if(entry['quote']){
html += '<div class="quote">';
$.each(entry['quote'],function(lineIndex,line){
html += '<div class="quote-line">' + line + '</div>';
});
if(entry['author']){
html += '<div class="quote-author">' + entry['author'] + '</div>';
}
html += '</div>';
}
html += '</div>';
html += '</div>';
});
執(zhí)行腳本
通過(guò) HTTP GET 請(qǐng)求載入并執(zhí)行一個(gè) JavaScript 文件
$.getScript('c.js',function(){
Alert(“加載完畢”);//回調(diào)函數(shù)
});
加載XML文檔
//通過(guò)遠(yuǎn)程 HTTP GET 請(qǐng)求載入信息。
$.get('d.xml',function(data){
$(data).find('entry').each(function(){
var $entry = $(this);
var html = '<div class="entry">';
html += '<h3 class="term">' + $entry.attr('term') + '</h3>';
html += '<div class="part">' + $entry.attr('part') + '</div>';
html += '<div class="definition">'
html += $entry.find('definition').text();
var $quote = $entry.find('quote');
if($quote.length){
html += '<div class="quote">';
$quote.find('line').each(function(){
html += '<div class="quote-line">' + $(this).text() + '</div>';
});
if($quote.attr('author')){
html += '<div class="quote-author">' + $quote.attr('author') + '</div>';
}
html += '</div>';
}
html += '</div>';
html += '</div>';
$('#dictionary').append($(html));
});
});
2.選擇數(shù)據(jù)格式
3.向服務(wù)器傳遞數(shù)據(jù)
4.關(guān)注請(qǐng)求
方法:
.ajaxStart()
.ajaxStop()
當(dāng)AJAX請(qǐng)求開(kāi)始且尚未進(jìn)行其他傳輸時(shí),會(huì)觸發(fā).ajaxStart()的回調(diào)函數(shù),相反,當(dāng)最后一次活動(dòng)請(qǐng)求終止時(shí),則會(huì)執(zhí)行通過(guò).ajaxStop()注冊(cè)的回調(diào)函數(shù)。
示例:
//當(dāng)請(qǐng)求時(shí),顯示#loading,結(jié)束時(shí),隱藏loading
$(document).ready(function(){
$('#loading').ajaxStart(function(){
$(this).show();
}).ajaxStop(function(){
$(this).hide();
});
});
5.Ajax和事件
限制函數(shù)綁定的作用域
$(document).ready(function(){
var bindBehaviors = function(scope){
$('h3',scope).click(function(){
$(this).toggleClass('highlighted');
});
};
bindBehaviors(this);
//這里的綁定作用域是document
$(document).ready(function(){
$('#letter-a .button').click(function(){
$('#dictionary').hide().load('a.html',function(){
bindBehaviors(this);
//這里的是文檔中的所以<h3>元素
$(this).fadeIn();
});
});
});
});
6.安全限制
posted on 2009-02-14 20:25
周銳 閱讀(529)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
Ajax