1. 程式人生 > >JS-屬性操作注意事項

JS-屬性操作注意事項

下列東西不能拿來做判斷:
1.所有的相對路徑
img.src
href=’1.css’
2.顏色值不要拿來做判斷:
color:red #f00 rgb(250,0,0)
3.innerHtml值不要拿來做判斷

注意事項:JS中允許“.”替換成“[]”
oDiv.style.width=’300px’; //.操作不能設為變數
oDiv.style[oAttr.value]=oval.value;//[]可任意修改

3.getElementById:前面只能用document
而其他類似的getElementsByTagName則可以用變數來。

4.for迴圈的注意點
for(var i=0;i<3;i++)
{
aLi[i].onclick=function(){
alert(i); //i=3
}
}

5.this的應用——————呼叫當前方法(函式)的那個物件
function fn1(){
alert(this);
}
fn1(); //this=window
oBtn.onclick=fn1; //this=oBtn
oBtn.onclick=function(){
this; //this=oBtn
fn1(); //this=window
}