javascript學習筆記

來源:互聯網
上載者:User

標籤:

輸出alertdocument.writedocument.getElementById("demo").innerHTML VARIANT 資料型別var lastname="Doe", age=30, job="carpenter";var cars=new Array();cars[0]="Saab";cars[1]="Volvo";cars[2]="BMW";var car = {type:"Fiat", model:500, color:"white"};js對象函數屬性  生命週期HTML 頁面完成載入HTML input 欄位改變時HTML 按鈕被點擊onchange     HTML 元素改變onclick    使用者點擊 HTML 元素onmouseover    使用者在一個HTML元素上移動滑鼠onmouseout    使用者從一個HTML元素上移開滑鼠onkeydown    使用者按下鍵盤按鍵onload    瀏覽器已完成頁面的載入\‘    單引號\"    雙引號\\    反斜線\n    換行\r    斷行符號\t    tab(定位字元)\b    退格符\f    換頁符var day=new Date().getDay();for - 迴圈代碼塊一定的次數for/in - 迴圈遍曆對象的屬性while - 當指定的條件為 true 時迴圈指定的代碼塊do/while - 同樣當指定的條件為 true 時迴圈指定的代碼塊var person={fname:"John",lname:"Doe",age:25}; for (x in person)  {  txt=txt + person[x];  }typeof "John"                // 返回 string typeof 3.14                  // 返回 numbertypeof false                 // 返回 booleantypeof [1,2,3,4]             // 返回 objecttypeof {name:‘John‘, age:34} // 返回 objectJavaScript 資料類型在 JavaScript 中有 5 中不同的資料類型:stringnumberbooleanobjectfunction3 種物件類型:ObjectDateArray2 個不包含任何值的資料類型:nullundefinedtry  {  //在這裡運行代碼  }catch(err)  {  //在這裡處理錯誤  }console.log() 方法表單驗證var x=document.forms["myForm"]["fname"].value;json的使用    var result="";                var obj1={                name:"zhangsan",                age:10,                habit:{                    tiyu:"籃球",                    shuiguo:"蘋果"                },                birthday:"2012",                child:[‘小米‘,‘校長‘]            };            // alert(typeof(obj1.child));            // alert(obj1.habit.shuiguo);            result="我的名字:"+obj1.name+",我今年:"+obj1.age+",我的愛好是:"+obj1.habit.tiyu+","+obj1.habit.shuiguo+",我的生日是:"+obj1.birthday+",我的孩子:"+obj1.child[0];            return result;// 建構函式:function myFunction(arg1, arg2) {    this.firstName = arg1;    this.lastName  = arg2;}// This creates a new objectvar x = new myFunction("John","Doe");x.firstName;     函數的定義 閉包解決  var json = [{dd:‘SB‘,AA:‘東東‘,re1:123},{cccc:‘dd‘,lk:‘1qw‘},{ccff:‘d11d‘}];// alert(json.length);for(var i=0;i<json.length;i++){for(var key in json[i]){// alert(key+‘:‘+json[i][key]);document.write(key+‘:‘+json[i][key]);}id 獲得var x=document.getElementById("intro");var x=document.getElementById("main");var y=x.getElementsByTagName("p");document.write(‘id="main"元素中的第一個段落為:‘ + y[0].innerHTML);<div id="main"><p> DOM 是非常有用的。</p><p>該執行個體展示了  <b>getElementsByTagName</b> 方法</p> 上面的例子示範了document.getElementById getElementsByTagName 使用getElementsByClassName 使用x=document.getElementsByClassName("intro");document.write("<p>文本來自 class 為 intro 段落: " + x[0].innerHTML + "</p>");
document.getElementsByClassName(‘te‘)[0].innerHTML="哈哈";

document.getElementById("image").src="landscape.jpg";
document.getElementById("p2").style.color="blue";
事件顯示隱藏
onclick="document.getElementById(‘p1‘).style.visibility=‘hidden‘"


 

onclick="this.innerHTML=‘Ooops!‘"


 

屬性添加方法
document.getElementById("myBtn").onclick=function(){displayDate()};
事件的例子:


 

當使用者點擊滑鼠時
當網頁已載入時
當映像已載入時
當滑鼠移動到元素上時
當輸入欄位被改變時
當提交 HTML 表單時
當使用者觸發按鍵時


 

nload 和 onunload 事件會在使用者進入或離開頁面時被觸發。


 

onload 事件可用於檢測訪問者的瀏覽器類型和瀏覽器版本,並基於這些資訊來載入網頁的正確版本。


 

onload 和 onunload 事件可用於處理 cookie。


 

轉成大寫
x.value=x.value.toUpperCase();
<input type="text" id="fname" onchange="myFunction()">


 

onmouseover="mOver(this)" onmouseout="mOut(this) 傳遞當前對象this


 

使用
<script>
function mOver(obj)
{
obj.innerHTML="Thank You"
}


 

function mOut(obj)
{
obj.innerHTML="Mouse Over Me"
}
</script>


 

onmousedown="lighton()" onmouseup="lightoff()"


 

onfocus="myFunction(this)


 

onmouseover="style.color=‘red‘


 

添加事件監聽
<script>
document.getElementById("myBtn").addEventListener("click", function(){
alert("Hello World!");
});
</script>


 

事件控制代碼允許同時添加多個事件
element.addEventListener("mouseover", myFunction);
element.addEventListener("click", mySecondFunction);
element.addEventListener("mouseout", myThirdFunction);


 

document.getElementById("myDiv").addEventListener("click", myFunction, true); 後面的true表示捕獲點擊
預設false 冒泡點擊


 

移除事件
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);


 

Math.random();隨機數


 


添加標籤
<script>
var para=document.createElement("p");
var node=document.createTextNode("This is new.");
para.appendChild(node);


 

var element=document.getElementById("div1");
element.appendChild(para);
</script>


 

刪除標籤
parent.removeChild(child);

 

 

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.