javascript開發中我們會經常獲取頁面中的事件對象,然后來處理這些事件,例如下面的getEvent函數就是獲取javascript下的頁面事件對象。
function getEvent(event){
return event || window.event;
}
裝文件public.js完整代碼:
(function(){
//用于得到一個dom元素
var $=function(id){
return document.getElementById(id);
};
//用于得到一個ajax對象
$.init=function(){
try{return new XMLHttpRequest()}catch(e){}
try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){}
alert('error');
};
//用于發送ajax的get請求
$.get=function(url,data,callback,type){
var xhr=$.init();
if(data!=null){
url=url+'?'+data;
}
xhr.onreadystatechange=function(){
if(xhr.readyState==4 && xhr.status==200){
if(type==null){
type='text';
}
if(type=='text'){
callback(xhr.responseText);
}
if(type=='xml'){
callback(xhr.responseXML);
}
if(type=='json'){
callback(eval('('+xhr.responseText+')'));
}
}
};
xhr.open('get',url);
xhr.setRequestHeader("If-Modified-Since","0");
xhr.send(null);
};
//用于發送ajax的post請求
$.post=function(url,data,callback,type){
var xhr=$.init();
xhr.onreadystatechange=function(){
if(xhr.readyState==4 && xhr.status==200){
//如果沒有傳遞type參數,讓type的值為text
if(type==null){
type='text';
}
if(type=='text'){
callback(xhr.responseText);
}
if(type=='xml'){
callback(xhr.responseXML);
}
if(type=='json'){
callback(eval('('+xhr.responseText+')'));
}
}
};
xhr.open('post',url);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send(data);
}
window.$=$;
})();
語言類似于jQuery
理解:
1、添寫一個自調用匿名函數
(function (){
})();
在一個項目中,可能會引用多個js框架,如果函數名相同,會有命名沖突,定義匿名函數沒有名稱,就不會沖突,
但匿名函數不能執行,所以需要使用以上格式讓其自動執行一次。
2、封裝一個函數,用于dom元素的獲取
var $=function(id){
return document.getElementById(id);
};
但$是局部變量,外面不能直接使用,所以:
window.$=$;
表示為全局對象window添加一個"$"的屬性,這個屬性的值是當前局部變量$的值。
所以在外部,如果想獲取某個dom元素,可以直接:
$('content');
3、封裝一個函數,用于創建ajax對象
因為之前,我們將一個函數賦值給了$,函數也是對象,所以$也就是一個對象
$.init=function(){
try{return new XMLHttpRequest()}catch(e){}
try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){}
alert('error');
};
表示為對象$添加一個新的方法:init
當然, 也可以添加其它方法
4、封裝ajax的get請求
為對象 $添加一個get方法,參數有三個
url:表示ajax請求的頁面地址
data:表示get請求時所需要傳遞的參數
callback:當ajax得到正確數據后,所執行的回調函數,也就是參數callback接收的參數應該是一個函數。
$.get=function(url,data,callback,type){
var xhr=$.init();
if(data!=null){
url=url+'?'+data;
}
xhr.open('get',url);
xhr.setRequestHeader("If-Modified-Since","0");
xhr.onreadystatechange=function(){
if(xhr.readyState==4 && xhr.status==200){
if(type==null){
type='text';
}
if(type=='text'){
callback(xhr.responseText);
}
if(type=='xml'){
callback(xhr.responseXML);
}
if(type=='json'){
callback(eval('('+xhr.responseText+')'));
}
}
};
xhr.send(null);
};
5、封裝post請求
為對象 $添加一個post方法,參數有三個
url:表示ajax請求的頁面地址
data:表示post請求時所需要傳遞的參數
callback:當ajax得到正確數據后,所執行的回調函數,也就是參數callback接收的參數應該是一個函數。
$.post=function(url,data,callback,type){
var xhr=$.init();
xhr.open('post',url);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onreadystatechange=function(){
if(xhr.readyState==4 && xhr.status==200){
//如果沒有傳遞type參數,讓type的值為text
if(type==null){
type='text';
}
if(type=='text'){
callback(xhr.responseText);
}
if(type=='xml'){
callback(xhr.responseXML);
}
if(type=='json'){
callback(eval('('+xhr.responseText+')'));
}
}
};
xhr.send(data);
}
當調用ajax請求時,可以使用這種形式:
$.method(頁面地址,傳遞參數,處理函數);
那么對應的方法中callback參數就指向了這個處理函數,所以
callback(xhr.responseText);
相當于
處理函數(xhr.responseText);
6、添加返回值類型
現在,我們在ajax程序中,可以使用三種數據形式:
1)字符串
2)xml
3)json
我們需要完善這個框架,可以返回不同類型的數據,再進行不同的處理。
首先,為get和post方法添加第四個參數:type,表示期望得到的返回值類型
$.post=function(url,data,callback,type){}
再根據type值的不同,返回對應的數據
if(type==null){
type='text';
}
if(type=='text'){
callback(xhr.responseText);
}
if(type=='xml'){
callback(xhr.responseXML);
}
if(type=='json'){
callback(eval('('+xhr.responseText+')'));
}
調用形式
$.method(請求地址,參數,處理函數,數據類型);
avascript使用document.getElementById操作div
javascript中經常會操作div,大家在網上看到的各種酷炫的前端效果,很多都是通過操作div來實現的,下面通過實例代碼和注釋來講解:
*請認真填寫需求信息,我們會在24小時內與您取得聯系。