JS-DOM:基礎知識(一)

來源:互聯網
上載者:User

標籤:des   style   color   java   os   io   strong   for   

1.childNodes 屬性可返回指定節點的子節點的節點列表(childNodes在firefox會選取空白節點)

<script>

window.onload=function(){

  var oUl=document.getElementById("ul1");

  alert(oUl.childNodes.length);//擷取子節點長度:11

}

</script>

<ul id="ul1">
    <li>我是文字1<span>我是span</span></li>
    <li>我是文字2</li>
    <li>我是文字3</li>
    <li>我是文字4</li>
    <li>我是文字5</li>
</ul>

2.children //dom選取頁面元素對象的子物件時,children 選取不包括空白節點

<script>

window.onload=function(){

  var oUl=document.getElementById("ul1");

  var aEl=oUl.getElementsByTagName("*");

  alert(oUl.children.length);

}

</script>

<ul id="ul1">
    <li>我是文字1<span>我是span</span></li>
    <li>我是文字2</li>
    <li>我是文字3</li>
    <li>我是文字4</li>
    <li>我是文字5</li>
</ul>

 

3.建立節點:1、createElement :建立標籤節點;2、 createTextNode :建立文本節點;

例一:<script>

window.onload=function(){

  var oBtn=document.getElementById("btn1");

  var oUl=document.getElementById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    var oTxt=document.createTextNode(index);

    oLi.appendChild(oTxt);

    oUl.apprendChild(oLi);

  }

}

</script>

<input type="button" value="按鈕" id="btn1">
<ul id="ul1">
    <li>0</li>
</ul>

 

例二:<script>

window.onload=function(){

  var oBtn=document.getElementById("btn1");

  var oUl=document.getElementById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    oLi.innerHTML=index;

    oUl.appendChild(oLi);

  }

}

</script>

<input type="button" value="按鈕" id="btn1">
<ul id="ul1">
    <li>0</li>
</ul>

 

4.setAttribute(name,value):設定節點上name屬性的值為value;

  getAttribute(name) :擷取節點上name屬性的值;

<script>

window.onload=function(){

  var oBtn=document.getElementById("btn1");

  oBtn.onclick=function(){

    oBtn.setAttribute("index","hello");

    alert(oBtn.getAttribute("index"));

  }

}

</script>

<input type="button" value="按鈕" index="1" id="btn1">

 

5、removeAttribute(Name): 刪除節點上的name屬性

<script>

window.onload=function(){

  var oBtn=document.getElementById("btn1");

  oBtn.onclick=function(){

    oBtn.removeAttribute("index");

  }

}

</script>

<input type="button" value="按鈕" index="1" id="btn1">

 

6、removeChild() :刪除子節點

<script>

function fnFirst(oParent){

  if(oParent.firstElementChild){

    return oParent.firstElementChild;

   }else{

      return oParent.firstChild;

    }  

}

window.onload=function(){

  var oBtn=document.getElementById("btn1");

  var oUl=document.getElementById("ul1");

  var oSpan=document.getElementById("span1");

  

  oBtn.onclick=function(){

    oUl.children[0].removeChild(oSpan);

  }

}

</script>

<input type="button" value="按鈕" id="btn1">
<ul id="ul1">
    <li>我是li1 <span id="span1">我是span</span></li>
    <li>我是li2</li>
    <li>我是li3</li>
    <li>我是li4</li>
</ul>

 

7、replaceChild(newNode,oldNode): newNode替換節點oldNode

<script>

function fnFirst(oParent){

  if(oParent.firstElementChild){

    return oParent.firstElementChild;

  }else{

    return oParent.firstChild;

  }

}

例子一:window.onload=function(){

  var oBtn=document.getElementById("btn1");

  var oUl=document.getElementById("ul1");

  oBtn.onclick=function(){

    oUl.replaceChild(oUl.children[3], oUl.children[1]);

  }

}

 

例子二: window.onload=function(){

  var oBtn=document.getElementById("btn1");

  var oUl=document.getElementById("ul1");

  oBtn.onclick=function(){

    var oDiv=document.createElement("div");

    oDiv.innerHTML="i am a div";

    oUl.replaceChild(oDiv, oUlchildren[1]);

  }

}

</script>

<input type="button" value="按鈕" id="btn1">
<ul id="ul1">
    <li>我是li1</li>
    <li>我是li2</li>
    <li>我是li3</li>
    <li>我是li4</li>
</ul>

 

8、appendChild(node): 向childNodes末尾插入一個節點node;

<script>

window.onload=function(){

  var oBtn=document.getElementById("btn1");

  var oUl=document.getElementById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    oLi.innerHTML=index;

    oUl.appendChild(oLi);

  }

}

</script>

<input type="button" value="按鈕" id="btn1">
<ul id="ul1">
    <li>0</li>
</ul>

 

9、insertBefore(node,targetNode): 向targetNode之前插入一個節點node

<script>

function fnFirst(oParent){

  if(oParent.firstElementChild){

    return oParent.firstElementChild;

  }else{

    return oParent.firstChild;

  }

}

方法一:window.onload=function(){

  var oBtn=document.getElementById("btn1");

  var oUl=document.getElementById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    oLi.innerHTML=index;

    oUl.insertBefore(oLi, fnFirst(oUl));

  }

}

 

方法二:window.onload=function(){

  var oBtn=document.getElementById("btn1");

  var oUl=document.getElmentById("ul1");

  var index=0;

  oBtn.onclick=function(){

    index++;

    var oLi=document.createElement("li");

    oLi.innerHTML=index;

    if(oUl.children.length>0){

      oUl.insertBefore(oLi,oUl.children[0]);

    }else{

      oUl.appendChild(oLi);

     }

  }

}

</script>

<input type="button" value="按鈕" id="btn1">
<ul id="ul1"></ul>

 

 

10、nextSibling: 下一個兄弟節點; previousSibling: 上一個兄弟節點

<script>

function getNext(obj){

  if(obj.nextElementSibling){

    return obj.nextElementSibling;

  }else{

    return obj.nextSibling;   

   }

}

 

function getPrevious(obj){

  if(obj.previousElmentSibling){

    return obj.previousElementSibling;  

  }else{

    return obj.previousSibling;

  }

}

 

window.onload=function(){

  var oUl=document.getElementById("ul1");

  var oLi=oUl.children[3];

  //alert(oLi.nextSibling.innerHTML);

  //alert(oLi.nextElementSibling.innerHTML);

  //alert(oLi.parentNode.id);

  var oParent=oLi.parentNode;

  alert(getNext(oParent).innerHTML);

}

</script>

<div id="div1">woshidiv1</div>
<ul id="ul1">
<li>我是文字1<span>我是span</span></li>
<li>我是文字2</li>
<li>我是文字3</li>
<li>我是文字4</li>
<li>我是文字5</li>
</ul>
<div id="div2">我是div2</div>

 

11、擷取計算後的樣式

 

(1)、currentStyle : ie所支援的擷取非行間樣式的方法

(2)、getComputedStyle: 非ie所支援的擷取非行間樣式的方法

 

<style type="text/css">
#div1{
    height: 100px;
    width: 100px;
    font-size: 12px;
    background-color: #ccc;
}
</style>
<script type="text/javascript">
function getStyle(obj,sStyle){

    if (window.getComputedStyle){
        return getComputedStyle(obj, null)[sStyle];
    }
    else{
        return obj.currentStyle[sStyle];
    }
}

window.onload=function (){

    var oDiv=document.getElementById("div1");

    oDiv.onclick=function (){

        // var fontSize=oDiv.style.fontSize;

        // alert(oDiv.currentStyle.fontSize);//在ie下擷取計算後的樣式
        // alert(getComputedStyle(oDiv, null).fontSize);
        alert(getStyle(oDiv,"fontSize"));

    }
}
</script>
</head>
<body>
<div id="div1" style="">123456</div>
</body>

聯繫我們

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