1.向表中追加行:
IE\FF:IE允许tr元素增加到tbody中,而不是直接增加到table中
<table id="myTable">
<tbody id="myTableBody"></tbody>
</table>
var cell=document.createElement("td").appendChild(document.createTextNode("foo");
var row = document.createElement("tr").appendChild(cell);
document.getElementById("mysqlTableBody").appendChild(row);
2.通过javascript设置元素的样式
FF:使用setAttribute方法
var spanElement = document.getElementById("mySpan"); spanElement.setAttribute("style","font-weight:bold ; color: red;");
IE\FF:setAttribute不起作用
var spanElement = document.getElementById("mySpan");
spanElement.style.cssText = "font-weight:blod ; color:red;";
/*或者spanElement.style.color = "red";*/
3.设置元素的class属性
FF:参数"class"
var element = document.getElementById("mySpan"); element.setAttribute("class","styleClass");
IE:参数"myClass"
var element = document.getElementById("mySpan"); element.setAttribute("className","styleClass");
IE\FF:将class和className都作为属性名
var element = document.getElementById("myElement"); element.setAttribute("class","styleClass"); element.setAttribute("className","styleClass");
4.创建输入元素
FF\IE:
var button = document.createElement("input"); button.setAttribute("type","button"); document.getElementById("formElement").appendChild(button);
5.向输入元素增加事件处理程序
FF:setAttribute方法
var formElement = document.getElementById("formElement"); formElement.setAttribute("onclick","doFun();");
FF\IE:
var formElement = document.getElementById("formElement"); formElement.onclick = function(){doFun();};
6.创建单选按钮
FF:
var readioButtion = document.createElement("input"); readioButtion.setAttribute("type","radio"); readioButtion.setAttribute("name","radioButtion"); readioButtion.setAttribute("value","checked");
IE:
var radioButtion = document.createElement("<input type='radio' name='radioButtion' value ='checked'>");
FF\IE:浏览器嗅探(browser-sniffing)机制,IE能识别出名为uniqueID的document对象的专用属性。
if(document.uniqueID){ //Internet Explorer var radioButtion = document.createElement("<input type='radio' name='radioButtion' value ='checked'>"); } else { //Standards Compliant var readioButtion = document.createElement("input"); readioButtion.setAttribute("type","radio"); readioButtion.setAttribute("name","radioButtion"); readioButtion.setAttribute("value","checked"); }