Java 之 JavaScript (二)

來源:互聯網
上載者:User

標籤:ie9   document   對象   更改   重新整理   out   多少   tno   nbsp   

1.DOM

  a.作用:通過 HTML DOM,可訪問 JavaScript HTML 文檔的所有元素

  b.功能:

    ①JavaScript 能夠改變頁面中的所有 HTML 元素

    ②JavaScript 能夠改變頁面中的所有 HTML 屬性

    ③JavaScript 能夠改變頁面中的所有 CSS 樣式

    ④JavaScript 能夠對頁面中的所有事件做出反應

 

2.擷取與更改

  a.擷取元素:

var x=document.getElementById("intro");    //通過id擷取元素var x=document.getElementByName("gender");    //通過name擷取元素var x=document.getElementsByClassName("test1");    //通過 類名 擷取元素var x=document.getElementsByTagName("input");    //通過 標籤名 擷取元素

 

  b.擷取與改變屬性:

var userName=document.getElementById("userName");//方法一    .屬性userName.style.display = "block";//方法二    get/setAttribute()userName.getAttribute("value");userName.setAttribute("value","test");//特殊方法——修改文本    .innerTestdocument.getElementById("id").innerTest= "hello word";//特殊方法——修改HTML語句    .innerHTMLdocument.getElementById("id").innerHTML= "<option>四川</option>";

    註:在IE9之前的版本中, .innerHTML不能在<option>及<table>中的<tr>、<td>中使用

 

 

3.事件綁定

  a.傳統方法

var userName=document.getElementById("userName");userName.onclick=function(){});

 

  b.現代方法

var userName=document.getElementById("userName");userName.addEventListener("click",function(){});    //添加事件userName.addEventListener("click",test /*方法名*/);    //刪除事件

 

  c.在IE8及以下版本無法使用.addEventListener,而使用:

var userName=document.getElementById("userName");userName.attachEvent("click",function(){    });    //添加事件userName.attachEvent("click",test /*方法名*/);    //刪除事件

 

 

4.節點

  a.方法:

var myUl = document.getElementById("myUl")var el = document.createElement("li");    //建立新的元素myUl.appendChild(el);    //添加子項目myUl.removeChild(el);    //添加子項目

  b.屬性:

myUl.parentNode    //擷取父級元素myUl.childNodes    //擷取子級元素myUl.firstChild    //第一個子項目myUl.lastChild    //最後一個子項目

 

 

5.BOM

  a.window 對象:瀏覽器中開啟的視窗

  b.方法:

alert();    //顯示帶有一段訊息和一個確認按鈕的警告框var flag =confirm("你確定要刪除這條資料嗎");    //顯示帶有一段訊息以及確認按鈕和取消按鈕的對話方塊,傳回值為 true 或 falseprompt("請輸入一個數字");    //顯示可提示使用者輸入的對話方塊focusTest.focus();    //把鍵盤焦點給予一個視窗focusTest.blur();    //把鍵盤焦點從頂層視窗移開open("http://www.baidu.com");    //開啟一個新的瀏覽器視窗或尋找一個已命名的視窗setInterval(function(){},1000);    //按照指定的周期(以毫秒計)來調用函數或計算運算式setTimeout(function(){},1000);    //在指定的毫秒數後調用函數或計算運算式clearInterval(idForInterval);    //取消由 setInterval() 設定的 timeoutclearTimeout(idForTimeout);    //取消由 setTimeout() 方法設定的 timeout

  c.屬性:

    ①location

location.reload();    //重新整理location.assign();    //載入新文檔(能返回)location.replace();    //載入新文檔(不能返回)location.href = "http://www.baidu.com"    //載入新文檔(建議使用)

    ②history

history.back();    //返回history.forward();    //前進history.go(2);    //跳轉多少頁

 

Java 之 JavaScript (二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.