標籤:簡單 cti 藝術 text set nod ade tee 建立對象
第七章:
1.dom方法建立並且插入標籤:(這種方法並沒有改變文檔的物理內容,而是在改變dom樹)
①建立元素節點:createElement();
②內部前插入:appendChild()
③建立文本節點:createTextNode();
④設定屬性節點:setAttribute();
⑤外部前插入:父元素.insertBefore(插入的元素,目標元素); 插入的元素被插入到,目標元素的前面。
⑥沒有inserAfter()函數。
2.小知識點:①appendChid(這裡面的元素不能加引號,否則是字串)
②ajax的onreadysatechange() 這個函數都是小寫。不知道為什麼不遵循駝峰的寫法,初寫時,一般會寫錯。
③在aja post時會佈建要求頭,即前序,其中Content-type 設定為application/x-www-form-urlencoded;即簡單的key-value的形式發送。
此外還可以設定為form-data,一般用於傳輸檔案。(比片等)。在工作中向伺服器傳輸圖片,是將圖片轉化成二進位流,然後post去發送,而Content-type則設定為form-data
3.ajax的過程,代碼:
1 var xmlhttp; 2 //平穩退化-對象檢測技術 3 //1.建立對象 4 if(window.XMLHttpRequest){ 5 xmlhttp = new XMLHttpRequest(); //適用於IE7+、FireFox、Chrome、Safari、Opera 6 }else{ 7 xmlhttp = new ActiveXObjext("Microsoft.XMLHTTP"); //適用於IE5、IE6 8 } 9 //2.確定請求類型、發送請求10 xmlhttp.open("GET","ajax1.php",true);11 xmlhttp.send();12 13 // xmlhttp.open("POST","t1.html",true);14 // xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");15 // xmlhttp.send("a=1&b=2");16 17 //3.響應請求(readyState 0-請求未初始化;1-與伺服器建立連結;2-請求已接受;3-請求處理中;4-請求處理完成)18 xmlhttp.onreadystatechange = function(){19 if(xmlhttp.readyState == "4" && xmlhttp.status == "200"){20 var p = document.createElement("p");21 var pText = document.createTextNode(xmlhttp.responseText);22 p.appendChild(pText)23 var body = document.getElementsByTagName("body")[0];24 body.appendChild(p) ;25 }26 }27 28 //說在最後:瀏覽器一般會禁止ajax使用file://協議。 提示跨域請求只支援http等協議 。 所以測試實現時,要安裝一個類似於phpstudy的軟體。讓自己的機器變成一台虛擬伺服器。
JavaScript DOM編程藝術-學習筆記(第七章)