day16 js+dom

來源:互聯網
上載者:User

標籤:js

表徵圖網站 http://fontawesome.io/icon

<i class="fa fa-sort" aria-hidden="true"></i>

3.21

函數

   匿名函數

   自執行函數  建立函數並自動執行

     (function(arg){  形參

                    console.log(arg);

               })(1) 實參

序列化  字串變數

   JSON.stringify()  序列化  從一個對象解析出字串

   JSON.parse()      反序化  字串中解析出json對象

轉義

- encodeURI( )                   URI中的逸出字元

- decodeURI( )                   URl中未轉義的字元

     將資料經過轉義後,儲存在cookie

val = eval(運算式)

時間

     var d = new  Date()

     d.getMinutes()

     d.setMinutes()     

範圍

範圍為可訪問變數,對象,函數的集合。


JavaScript 變數生命週期在它聲明時初始化。

局部變數在函數執行完畢後銷毀。

全域變數在頁面關閉後銷毀。


  1. 以函數作為範圍

  2. 函數的範圍在函數未被調用之前,就已經建立

  3. 函數的範圍 存在 範圍鏈,並且也是在被調用之前建立。。先找自己的,自己沒有找上一層,沒有再找外層。

  4. 函數內的局部變數會提前聲明,不進行賦值


 function func() {

     var xo = ‘hequan‘;

     function inner() {

         console.log(xo);

     }

     return inner;

 }

var ret = func()

ret()

    hequan

 function func() {

     console.log(xo);

     var xo = "hequan";

     }

func()

     undefined

物件導向

function foo(n) {

    this.name=n;

    this.sayName = function () {

        console.log(this.name)

    }

}

var obj1 = new foo("hq")

obj1.name

obj1.sayName()

     原形:

function  Foo(n) {

    this.name = n;

}

Foo.prototype = {

    ‘sayName‘:function () {

        console.log(this.name)

    }

}

obj1 = new Foo(‘we‘);

obj1.sayName()

尋找

     直接尋找

     var obj = document.getElementById(‘I1‘)

     間接尋找

     innerText    文本

     innerHtml    全內容

     obj.value   擷取值

     樣式操作

     .className  

     .classList.add  .remove

     .style.color 

     

     屬性操作

     obj.setAttribute("屬性","值")

     建立標籤 並添加到HTML

           字串

           insertAdjacentHTML("beforeEnd",obj)

           

           對象的方式

              document.createElement(‘div‘)

     提交表單

          document.geElementById(‘form‘).submit()

   

     其他

console.log                 輸出框

alert                       彈出框

confirm                     確認框  返回true false

// URL和重新整理

location.href               擷取URL

location.href = "url"       重新導向

location.reload()           重新載入

// 定時器

setInterval                 多次定時器

clearInterval               清除多次定時器

setTimeout                  單次定時器

clearTimeout                清除單次定時器

     事件

          onclick,onblur,onfocus

          

document.getElementById             根據ID擷取一個標籤

document.getElementsByName          根據name屬性擷取標籤集合

document.getElementsByClassName     根據class屬性擷取標籤集合

document.getElementsByTagName       根據標籤名擷取標籤集合

綁定事件兩種方式:

     直接綁定   onclick="xx()"

     先擷取DOM對象,然後進行綁定

<div  id = ‘test‘><button type="button">Click Me!</button></div>

<script>

    var mydiv = document.getElementById(‘test‘)

    console.log(mydiv)

    mydiv.onclick = function () {

        console.log(‘asdw‘);

    }

</script>

this  當前觸發事件的標籤

第一種綁定方式

 onclick="Chang(this)"

function  Chang(self){

     //self 當前點擊的標籤

}

第二種綁定方式

<input   id=‘i1‘ type=‘button‘>

document.getElementById(‘i1‘).onclick = function(){

     //this  代之當前點擊的標籤

}


本文出自 “何全” 部落格,請務必保留此出處http://hequan.blog.51cto.com/5701886/1909384

day16 js+dom

聯繫我們

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