JavaScript 聲明變量是 直接用var 后邊可以寫int類型 string類型等
HTML用來編排內(nèi)容? CSS用來制定樣式 JavaScript用來控制行為
那么怎么控制網(wǎng)頁呢?
通過DOM(Document Object Model)-文檔對象模型 可以能使HTML和JavaScript緊密的結合在一起(這里是把每一個標簽看成一個對象 從而組成了這個模型)
例:
html代碼:
<div>
<p id="p1">段落一</p>? //id 代表了每個標簽的內(nèi)容
<p id="p2">段落二</p>
</div>
JavaScript代碼:
var p1=document.getElementById('p1');? //這里的p1獲取了html里id為p1標簽的內(nèi)容
var p2=document.getElementById('p2');? //這里的p2獲取了html里id為p2標簽的內(nèi)容
p2.innerHTML="我是小仙女";? //這里p2變化時html中標簽為p2的內(nèi)容也變?yōu)椤拔沂切∠膳?/p>
在JavaScript中函數(shù)有這一說 我把它理解為C#的方法? 但是它跟Lua的方法書寫方式就是一樣的 都是function 例:
function fun(a,b,c,d)
{
var sun=a*b*c*d
alert(sun);
}
fun(1,2,3,4);? //感覺 JavaScrip 跟lua一樣的是不用聲明int等類型
JavaScript的監(jiān)聽事件:
<button id="click">點擊我</button>? //html代碼
var? b1=document.getElementById('click'); //JavaScript代碼
function fun()
{
alert("我被點擊了");
}
b1.onclick=fun;
//相當于unity中Button的點擊事件? onclick 鼠標點擊時事件響應、 onmouseover 鼠標移動到元素事件響應、onmouseup 鼠標松開時事件響應,onmouseover 當鼠標移到button上時事件響應
JavaScript實現(xiàn)用戶輸入框?
例:
<input id="input">? //html代碼? ? input輸入
<button id="button">確定</button>? button按鈕
var input=document.getElementById("input");? //JavaScript代碼
var button=document,getElementById("button");
button.onclick=fun;
function fun()
{
alert(input.value);
}
JavaScript的代碼寫在html<body> 后的<script></script>中
今天寫到這里了 明天周一 上班加油