Dom操作技术《javascript高级程序设计》阅读笔记
菜鸟记录书中的基础知识,以便日后复习:
1,动态脚本:
function loadScriptString(code){
var script=document.createElement("script");
script.type="text/javascript";
try{
script.appendChild(document.cteateTextNode(code));//ie中会发生异常
}catch(ex){
script.text=code;
}
}
2,动态样式:
function loadStyleString(css){
var style=document.createElement("style");
style.type="text/css";
try{
style.appendChild(document.cteateTextNode(css));//ie中会发生异常
}catch(ex){
style.styleSheet.cssText=css;
}
var head=document.getElementByTagName("head")[0];
head.appendChild(style);
}
3,操作表格:
假如要创建下面的HTML表格:
<table border="1" width="100%">
<tbody>
<tr>
<td>Cell 1,1</td>
<td>Cell 2,1</td>
</tr>
<tr>
<td>Cell 1,2</td>
<td>Cell 2,2</td>
</tr>
</tbody>
</table>
var table=document.createElement("table");
table.border=1;
table.width=100%;
var tbody=document.createElement("tbody");
table.appendChild(tbody);
//创建第一行
tbody.insertRow(0);
tbody.row[0].insertCell(0);
tbody.row[0].cells[0].appendChild(document.createTextNode("Cell 1,1");
tbody.row[0].insertCell(1);
tbody.row[0].cells[1].appendChild(document.createTextNode("Cell 2,1");
//创建第二行
tbody.insertRow(1);
tbody.row[1].insertCell(0);
tbody.row[1].cells[0].appendChild(document.createTextNode("Cell 1,2");
tbody.row[1].insertCell(1);
tbody.row[1].cells[1].appendChild(document.createTextNode("Cell 2,2");
document.body.appendChild(table);