總結AJAX相關JS程式碼片段和瀏覽器模型_javascript技巧

來源:互聯網
上載者:User
在.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 方法等,在不同需要下使用不同方法,前提是研究好瀏覽器模型 的各種對象的方法屬性。尤其是熟悉CSS+HTML就會做的很酷。就JS語言本身來說要求不高。

  以下是以Document對象為例,相關方法有:

Method Description 
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 
相關文章

聯繫我們

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