DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解
AJAX 與瀏覽器模型:常用 DOM 操作技巧
編輯:AJAX詳解     

在 .Net 開發中,善用免費控件本是好事,但當控件無法修改到滿足需求時,就得動用 JS 大法——前提是先研究透徹瀏覽器模型中各種物件的方法與屬性。尤其熟悉 CSS+HTML,做起來就會很酷;就 JS 語言本身而言,要求其實不高。

常用簡短代碼技巧

1. 動態刪除 Table 內容,一行即可:

tb.removeNode(true)

2. 動態增加行,除了 createElement,還可用更簡短的方式:

<table id=tb1></table>
<SCRIPT>
function addTable(){
    var row1 = tb1.insertRow();
    var cell1 = row1.insertCell();
    var cell2 = row1.insertCell();
    cell1.innerText = "灰豆寶寶";
    cell2.innerText = "超級大笨狼";
}
</SCRIPT>
<INPUT TYPE="button" VALUE="AddTable" onclick="addTable()">

3. 在 DIV 中動態增加 Table:

<SCRIPT>
function addTable(){
    var tb1 = document.createElement("table");
    tb1.border = "1px";
    var row1 = tb1.insertRow();
    var cell1 = row1.insertCell();
    var cell2 = row1.insertCell();
    mydiv.appendChild(tb1);
    cell1.innerText = "wanghr100";
    cell2.innerText = "panyuguang962";
}
</SCRIPT>
<BODY>
<div id=mydiv style="width:400;height:300;"></div>
<INPUT TYPE="button" VALUE="AddTable" onclick="addTable()">

4. 在 DIV 中刪除 Table,簡單地令 Div.innerHtml="" 即可。

以上是部分實用而簡短的代碼,當然也有其他實現方式,只是通常更長,例如組合使用 DIV 的 insertAdjacentHTML 方法。不同需求下選用不同方法,關鍵仍在於吃透瀏覽器模型的各物件方法與屬性。

Document 物件的相關方法

attachEvent、createAttribute、createComment、createDocumentFragment、createElement、createEventObject、createStyleSheet、createTextNode、detachEvent、getElementById、getElementsByName、getElementsByTagName、mergeAttributes、recalc、write、writeln。

DIV 物件的相關方法

addBehavior、appendChild、applyElement、attachEvent、clearAttributes、cloneNode、contains、detachEvent、getAdjacentText、getAttribute、getAttributeNode、getElementsByTagName、hasChildNodes、insertAdjacentElement、insertAdjacentHtml、insertAdjacentText、insertBefore、mergeAttributes、normalize、removeAttribute、removeAttributeNode、removeBehavior、removeChild、removeExpression、removeNode、replaceAdjacentText、replaceChild、replaceNode、setActive、setAttribute、setAttributeNode、setExpression。

跨瀏覽器相容性提醒

實作相同功能、相對簡短的精彩代碼值得收藏。以下幾點相容性問題尤需注意:

1. removeNode(true) 並非所有非 IE 瀏覽器都支援,跨瀏覽器應改用 obj.parentNode.removeChild(obj)。

2. insertRow(x)、insertCell(y) 的參數在 IE 中可省略,但在非 IE 瀏覽器中不可省略。

3. insertAdjacentElement、innerText 等屬於 IE 特有方法,並非 W3C 標準,使用時務必留意。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved