JavaScript 的學習程序整理(…

<!-- // JavaScript Document -->

<!DOCUMENT html>

<html>


<head>

javascript可以寫在這裏 (頭部 )。

</head>


<!-- οnlοad="" -->


<body  οnlοad="CheckCookies()">

<h1 οnclick="this.innerHTML='Come On !'"> Javascript Note :</h1>

<p>

腳本可位於 HTML 的  body  或  head  部分中,或者同時存在於兩個部分中;<br>


"script" 和  "/script" 會告訴 JavaScript 在何處開始和結束!<br>


在 JavaScript 中,用分號來結束語句是可選的。<br>


JavaScript 語句通過代碼塊的形式進行組合。塊由左花括號開始,由右花括號結束。塊可使語句序列一起執行。<br>


JavaScript 對大小寫是敏感的 ; 忽略多餘的空格 <br>


Undefined 這個值表示變量不含有值。可以通過將變量的值設置爲 null 來清空變量。<br>


對象是數據(變量),擁有屬性和方法。<br>


JavaScript 能夠直接寫入 HTML 輸出流中,並且可以修改其內容和格式 ;<br>


能夠對事件做出反應(比如對按鈕的 click: );<br>


可以驗證用戶輸入;<br>


onload 和 onunload 事件會在用戶進入或離開頁面時被觸發。<br>


onchange 事件常結合對輸入字段的驗證來使用。<br>


onmouseover 和 onmouseout 事件可用於在用戶的鼠標移至 HTML 元素上方或移出元素時觸發函數。<br>


onmousedown, onmouseup 以及 onclick 構成了鼠標點擊事件的所有部分。首先當點擊鼠標按鈕時,會觸發 onmousedown 事件,當釋放鼠標按鈕時,會觸發 onmouseup 事件,最後,當完成鼠標點擊時,會觸發 onclick 事件。<br>


onfocus當輸入字段獲得焦點時,改變其背景色。<br>


.length()可計算字符串的長度<br>?

.big .blink .bold  and so on 可以改變字符串的樣式<br>

.indexof()可以定位字符串中某一個字符首次出現的位置<br>

.match()查找特定字符<br>




注://  的註釋方法在Javascript Editor 中不起作用 "<!--  -->"

<em><br></em>是換行符 

</p>


<div οnmοuseοver="mOver(this)" οnmοuseοut="mOut(this)" 

style="background-color:pink;

width:220px;

height:5px;

padding:40px;

color:#ffffff;">把鼠標移到上面</div><br>



<div οnmοuseοver="style.color='red'" οnmοuseοut="style.color='blue'">please put the mouse on it!</div><br>


<div id="txt"></div>


<div οnmοusedοwn="mDown(this)" οnmοuseup="mUp(this)" 

style="background-color:blue;

color:#ffffff;

width:180px;

height:20px;

padding:40px;

font-size:20px;">請點擊這裏 </div>



<p id="demo0">

JavaScript can change the content of HTML!

</p>



<p id="demo">

JavaScript can change the content of HTML!

</p>




<!-- Javascript 的開始標記 -->

<script> 

document.write("<h1>Title No 1</h1>");   //寫入HTML元素

document.write("<h2>Title No 2</h2>");    //不同的head 字體大小不同

document.write("<h3>Title No 3</h3>");

document.write("<p>This is a paragraph.</p>");


document.write(Date()+"<br>");  //返回當前時間


var d=new Date();

document.write("從 1970/01/01 至今已過去 " + d.getTime() + " 毫秒"+"<br>");   //計算時間差


d.setFullYear(1992,10,3)      //設置具體時間

document.write(d+"<br>");


var weekday=new Array(7)

weekday[0]="星期日"

weekday[1]="星期一"

weekday[2]="星期二"

weekday[3]="星期三"

weekday[4]="星期四"

weekday[5]="星期五"

weekday[6]="星期六"

document.write("今天是" + weekday[d.getDay()]+"<br>")



//document.write(startTime()+"<br>");




// 注意要把  <p  id = "demo">....</p>先定義

document.getElementByIdx_x("demo").style.color="#ff0000";  //字體變爲紅色

document.getElementByIdx_x("demo0").style.color="blue";

document.getElementByIdx_x("demo0").style.frontSize="larger";

document.getElementByIdx_x("demo0").style.frontFamily="Arial";



var txt="Hello World!";


document.write("<p>Big: " + txt+ "</p>");

document.write("<p>Big: " + txt.big() + "</p>")

document.write("<p>Small: " + txt.small() + "</p>")


document.write("<p>Bold: " + txt.bold() + "</p>")

document.write("<p>Italic: " + txt.italics() + "</p>")


document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>")

document.write("<p>Fixed: " + txt.fixed() + "</p>")

document.write("<p>Strike: " + txt.strike() + "</p>")


document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")

document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")


document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")

document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")


document.write("<p>Subscript: " + txt.sub() + "</p>")

document.write("<p>Superscript: " + txt.sup() + "</p>")


document.write("<p>Link: " + txt.link("http://www.w3school.com.cn") + "</p>")


document.write("<p>tex字符串的長度是:"+txt.length+"</p>");


document.write("<p>查找串 World的首次出現位置:"+ txt.indexOf("World")+"</p>");

document.write("<p>查找串 world的首次出現位置:"+ txt.indexOf("world")+"</p>");//對大小寫敏感


document.write("<p>匹配字符串 llo:"+ txt.match('llo')+"</p>");

document.write("<p>匹配字符串 world:"+ txt.match('world')+"</p>");


document.write("<p>用zhaodeng替換字符串 World:"+txt.replace(/World/, "zhaodeng")+"</p>");





<!-- 如需向 HTML DOM 添加新元素,您必須首先創建該元素(元素節點),然後向一個已存在的元素追加該元素。 -->

var para=document_createElement_x("p");

var node=document_createTextNode("這是新段落。");

para.a(node);

var element = document.getElementByIdx_x("demo0");

element.a(para);



<!-- 如需刪除 HTML 元素,您必須首先獲得該元素的父元素: -->

var parent=document.getElementByIdx_x("demo0");

var child=document.getElementByIdx_x("p");

parent.removeChild(child);







<!-- Javascript的變量、數組 -->

var i;

var x=      new Number;  <!--可用new 來聲明變量的類型 變量的類型有 String\Number\Boolean\Array\Object (pay attention to the case(大小寫) )-->

document.write(i + "<br>");

var i= null;

document.write(i + "<br>");

var person = new Array();

person[0]="Ruiping Zhang";

person[1]="Deng Zhao ";

for(i=0; i<person.length; i++)

{

document.write( person[i]+"<br>");

}




<!-- Javascript的對象 ,有點類似C++中的類(結構體 ) -->

{

var student={

StudentId : "1004101206",

studentName  : "趙登",

Major        :" Computer Science and technology",

scord        :3.46

};

<!-- 調用小寫到大寫的轉換函數 toUpperCase() -->

document.write(student.StudentId + "<br />");

document.write(student["studentName"] + "的學號是" +student.StudentId +",專業是"+ student.Major.toUpperCase()+", 成績是  "+student.scord+"<br />"); 

}






function changeHTML()

{

x=document.getElementByIdx_x("demo");

x.innerHTML="changed: Hello Javascript !";

}

<!-- 下行代碼等價於changeHTMl()函數  -->

document.getElementByIdx_x("demo").innerHTML= "HTML 元素可以改變 !";



function IfNaN()

{

var x= document.getElementByIdx_x("id2").value;

if(  isNaN(x))  alert("Not Numeric !!!");

else if (x== "" ) alert ("please input the number !");

else   alert("  Correct Input!");

}




<!-- 帶參函數 -->

function Welcome(hellowords , name)

{

alert(hellowords +  name );

}



<!-- 帶有返回參數的函數 -->

function countAge(a)

{

 return a+1;

}




function CheckCookies()

{

 if (navigator.cookieEnabled==true) alert("已啓用cookie!");

 else alert ("未啓用 cookies!");

}




function OnChange()

{

  

  var z= document.getElementByIdx_x("id3")

  z.value= z.value.toUpperCase();

}



function OnFocus(x)

{

 x.style.background="yellow";

}



function mOver(obj)

{

obj.style.backgroundColor="orange";

obj.style.color="black";

obj.innerHTML="謝謝"

}



function mOut(obj)

{

obj.style.color="white";

obj.innerHTML="把鼠標移到上面"

}




function mDown(obj)

{

obj.style.backgroundColor="yellow";

obj.style.color="black";

obj.innerHTML="請釋放鼠標按鈕"

}



function mUp(obj)

{

obj.style.backgroundColor="green";

obj.innerHTML="請按下鼠標按鈕"

}



function startTime()

{

var today=new Date()

var h=today.getHours()

var m=today.getMinutes()

var s=today.getSeconds()

// add a zero in front of numbers<10

m=checkTime(m)

s=checkTime(s)

document.getElementByIdx_x('txt').innerHTML=h+":"+m+":"+s

t=setTimeout('startTime()',500)

}


function checkTime(i)

{

if (i<10) 

  {i="0" + i}

  return i

}



function DOCUMENT()

{

 document.write("fuck , the  document disappeared!!");

}


</script>

<!-- Javascript 的結束 -->




<button type="button"  οnclick="alert('Welcome!')" >點擊這裏</button><br>  <!-- ( 彈窗 ) -->



<button type="button"  οnclick="changeHTML()" >點擊改變HTML元素</button><br>  <!--  按鈕點擊調用函數 -->



<p>please input a number ,if the input is not a number ,there will be an alert !</p>

<input id = "id2" type="text">

<button type="button" onclick ="IfNaN()">點擊驗證是否是數字</button><br>




<button type = "button" οnclick="Welcome('How are you? ', '趙登')">點擊問候 </button>


<button type = "button" οnclick="Welcome('What is the matter? ', '瓶子')">點擊問候 </button><br>




<button type="button" οnclick="alert('I will be '+countAge(22)+' years old in the next year! ')">點擊查看變化</button><br>



please input a English String :<input type ="text" id = "id3" onchange ="OnChange()" οnfοcus="OnFocus(this)"/>

<p>當您離開輸入字段時,會觸發將輸入文本轉換爲大寫的函數。</p>



<p id="id5">點擊可隱藏的文本  </p>

<input type="button" value="點擊可隱藏文本" οnclick="document.getElementByIdx_x('id5').style.visibility='hidden'" />

<input type="button" value="點擊顯示文本" οnclick="document.getElementByIdx_x('id5').style.visibility='visible'" /><br>



<!--  <button type="button" οnclick="startTime()">點擊顯示鐘錶時間</button>-->


<!-- 如果在文檔已完成加載後執行 document.write,整個 HTML 頁面將被覆蓋: -->

<button  type = "button "  οnclick="DOCUMENT()"> DOCUMENT.WRITE TO THE WHOLE HTML page</button>



</body>



</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章