9.7 js進階總結2

來源:互聯網
上載者:User

標籤:

數組元素添加

將一個或多個新元素添加到數組結尾,並返回數組新長度

var week_len = week.push(‘星期四’,‘星期五’);

將一個或多個新元素添加到數組開始,數組中的元素自動後移,返回數組新長度

var week_len = week.unshift(‘星期日’);

將一個或多個新元素插入到數組的指定位置,插入位置的元素自動後移,返回""。

arr3.splice(0,0,“abc”);

document.write(arr3[0]);

數組元素刪除

var del = ["aa",23,345,56,34,"bb"];

var del_last = del.pop();        //移除最後一個元素並返回該元素值

var del_first = del.shift();      //移除最前一個元素並返回該元素值,數組中元素自動前移

var del_arr = del.splice(0,2); //刪除從指定位置deletePos開始的指定數量deleteCount的元素,數組形式返回所移除的元素

document.write(del_arr);

string對象

string對象用來處理文本。

事實上任何一個字串常量都是一個string對象,可以將其直接作為對象來使用。

獲得字串長度:

length屬性:

var str= “hello”,

str2= “你好”;

alert(str.length);

alert(str2.length);

 

提取字串:

charAt()方法

var str= “HELLO WORLD”,

n = str.charAt(2);

 

substr()方法

var str = ‘hello’;

alert(str.substr(0,2);

 

substring()方法

alert(str.substring(0,3))

尋找替換字串:

indexOf()和lastIndex()方法

var str="Hello world, welcome to the universe.";

alert(str.indexOf(“welcome”);

alert(str.lastIndexOf(‘e’));

 

replace()方法

var str="Visit Microsoft!";

var n=str.replace("Microsoft","W3CSchool");

 

search()方法

var str="Visit W3CSchool!";

var n=str.search(“W3CSchool”);

window對象

window對象表示整個瀏覽器視窗

定時器setInterva() , setTimeout()

定時器可以說是js前端最常用的工具,幾乎所有的逐漸層化的動態效果都會使用到定時器,比如說圖片滾動,漸隱漸現,拖拽等等.定時器分兩種分別是settimeout和setinterval.

window.setInterval();   設定迴圈定時器

var T = window.setInterval(test,1000);  test:執行的代碼串或函數  設定1000毫秒

 

window.clearInterval();  清除迴圈定時器

window.clearInterval(T);

 

window.setTimeout();    設定單次定時器

var T = setTimeout(test,1000);  test:執行的代碼串或函數   設定1000毫秒

 

window.clearTimeout()    清除單次定時器

clearTimeout();

history對象

history對象是window對象的子物件,對應於瀏覽器的記錄。

window.history.go(-1);

window.history.go(1);

 

history.back();

history.forward(); 

Location對象

Location對象也是window對象的子物件,通過它可以擷取或設定瀏覽器的當前地址。

1.跳轉到其他頁面

window.location.href = "http://www.163.com";

location.href = "http://www.163.com";

2.重新載入頁面(重新整理)

location.reload();

navigator對象

navigator對象包含著有關web瀏覽器的資訊,它也是window的屬性,可以用window.navigator引用它,也可以用navigator引用

var info= navigatorAgent;

alert(info);

9.7 js進階總結2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.