HTML5學(xué)習(xí)小記二ajax,window.orientation,prototype.forEach

1.ajax的學(xué)習(xí)

HTML5中的ajax相當(dāng)于iOS中的afnetworking;詳見jQuery ajax - ajax() 方法;

對ajax使用post請求的demo練習(xí):

function testPost(){

$.ajax({

type:"POST",//請求的類型 POST GET 無大小寫區(qū)分

url:"http://www.test.com",

data:{name:"post",phone:"3112122"},//post請求的追加參數(shù),與iOS中的字典相同;

datatype: "html",//返回數(shù)據(jù)的格式"xml", "html", "script", "json", "jsonp", "text".

beforeSend:function(){$("#msg").html("logining");},//請求之前調(diào)用的函數(shù)

success:function(data){ ?//請求成功返回調(diào)用的函數(shù)

$("#msg").html(decodeURI(data));

}? ,

//調(diào)用執(zhí)行后調(diào)用的函數(shù)

complete: function(XMLHttpRequest, textStatus){

alert(XMLHttpRequest.responseText);

alert(textStatus);

//HideLoading();},

success :function(data){

//請求成功的處理方法

}

error: function(){

//請求出錯處理

}});}

對ajax的get請求的說明:

url的不同: ? ?url = "update.php?username=" +encodeURIComponent(username) + "&content=" +encodeURIComponent(content)+"&id=1" ;

在ajax請求時 防止異步請求的方法

//var imgNum = $('img').length;

//$('img').load(function() {

//if (!--imgNum) {

//}

//});

2.獲取到網(wǎng)頁滑動的方法,當(dāng)滑動到底部的時候進行加載下一頁

$(window).scroll(function() {

var scrollTop = $(this).scrollTop();//方法返回或元素的滾動條的垂直位置。

var scrollHeight = $(document).height();//整篇文章的高度

var windowHeight = $(this).height();//是獲取當(dāng)前 也就是你瀏覽器所能看到的頁面的那部分的高度? 這個大小在你縮放瀏覽器窗口大小時 會改變 與document是不一樣的? 根據(jù)英文應(yīng)該也能理解吧

if(scrollTop + windowHeight >= scrollHeight) {

orderid = $("#orderidHidden").val();

getdata(usertype, notvip, orderid);//執(zhí)行的請求方法

}

});

3. ?關(guān)于window.addeventlistener

當(dāng)對一個對像綁定多個方法的時候,之后最后一個會生效,使用addeventlistener之后,每一個方法都是可以執(zhí)行;用來解決讓一個js事件執(zhí)行多個函數(shù)

4.關(guān)于 window.orientation

window.addEventListener("onorientationchange" in window ? "orientationchange":"resize",function(){

if (window.orientation===180 ||window.orientation===0){

$('.lock_wrp').CSS("display","none");

console.log('這個是豎屏展示');}

if (window.orientation===90 ||window.orientation===-90){

$('.lock_wrp').css("display","block");

console.log('這個是橫屏展示');

e.preventDefault();

});}});

屏幕旋轉(zhuǎn)事件: onorientationchange ? 出自 html5屏幕旋轉(zhuǎn)事件 onorientationchange

function orientationChange() { ?

?switch(window.orientation) {? ??

  case 0:? ? ? ? ?

?? alert("肖像模式 0,screen-width: " + screen.width + "; screen-height:" + screen.height);? ? ? ? ? ? break;??

?   case -90:? ??

? ? ? ? alert("左旋 -90,screen-width: " + screen.width + "; screen-height:" + screen.height);? ? ? ? ? ? break;? ??

  case 90:? ? ? ? ??

? ? alert("右旋 90,screen-width: " + screen.width + "; screen-height:" + screen.height);? ? ? ? ? ? break;? ?

 case 180:? ? ? ? ?

 alert("風(fēng)景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height);? ? ? ?   break;? ? };

};// 添加事件監(jiān)聽addEventListener('load', function(){? ? orientationChange();? ? window.onorientationchange = orientationChange;});

5.removeAttr() 方法從被選元素中移除屬性。

6.關(guān)于遍歷數(shù)組的幾種方法;參考:Array.prototype.forEach數(shù)組遍歷

if(!Array.prototype.forEach){

Array.prototype.forEach = function(callback,thisArg){

var T, k;

if(this === null){

throw new TypeError("this is null or not defined")

}

var O = Object(this);

var len = O.length >>> 0;

if(typeof callback !== "function"){

throw new TypeError(callback + " is not a function");

}

if(arguments.length > 1){

T = thisArg;

}

k = 0;

while(k < len){

var kValue;

if(k in O){

kValue = O[k];

callback.call(T,kValue,k,O);

}

k++;

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 常見試題 行內(nèi)元素:會在水平方向排列,不能包含塊級元素,設(shè)置width無效,height無效(可以設(shè)置line-h...
    他大舅啊閱讀 2,467評論 1 5
  • 單例模式 適用場景:可能會在場景中使用到對象,但只有一個實例,加載時并不主動創(chuàng)建,需要時才創(chuàng)建 最常見的單例模式,...
    Obeing閱讀 2,092評論 1 10
  • 1.JQuery 基礎(chǔ) 改變web開發(fā)人員創(chuàng)造搞交互性界面的方式。設(shè)計者無需花費時間糾纏JS復(fù)雜的高級特性。 1....
    LaBaby_閱讀 1,367評論 0 2
  • 這本繪本買的很勉強,看名字,就覺得會是一個很俗套的森林冒險故事。不過,是我的偶像推薦的,出于對偶像的盲目信任,...
    我想我是豬懶豬閱讀 1,353評論 0 0
  • 每道美食都該有她自己的故事 我的公司離市區(qū)很遠,所以在不認識高姐之前,每天中午都為了吃什么而發(fā)愁。自從認識高姐以后...
    野廚橙先生閱讀 2,797評論 54 29