關於javascript操作html

來源:互聯網
上載者:User
/** * 這裡我們簡單介紹一下使用javascript操作html元素 * 我們可以通過document.getElementById("998");擷取一個網頁上的元素 */window.onload=init;function init(){var btn=document.getElementById("998"); //擷取一個按鈕var img=document.getElementById("img1");//擷取一個圖片img.src="js/c.jpg";  //使用代碼設定圖片的src 類似<img id='img1' src='js/c.jpg'/>img.title="圖片翻轉"; //直接使用字串賦值img.className="hide";alert(img.onclick);//可以查看html產生的資訊img.onclick=onclick; //屬性和事件是不一樣的設定方法,需要賦值一個函數img.onmouseover=onmouseover;img.onmouseout=onmouseout;btn.innerHTML="顯示圖片";btn.onclick=btn_click;}function onclick(event) {    alert(1234);   }function onmouseover(event) {    this.src = "js/b.jpg";//千萬要記得加上this 否則src預設為windows對象的屬性 ,起不了任何作用}function onmouseout(event) {    this.src = "js/c.jpg";//對於屬性值需要明確指定,就像java中的this指標一樣。屬性依賴於對象而存在。同樣,這個方法也是依賴於對象而存在的}function btn_click(){//對於事件方法不能傳遞參數,貌似。所有我們可以使用this調用當前對象。對於其他元素需要擷取if("顯示圖片"==this.innerHTML){var img=document.getElementById("img1"); //如果需要使用其他的元素的話,我們可以擷取,img.className="show";this.innerHTML="隱藏圖片";}else if("隱藏圖片"==this.innerHTML){var img=document.getElementById("img1");img.className="hide";this.innerHTML="顯示圖片";}}

聯繫我們

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