1. 向表中追加行
这是IE和Firefox,Safari,Opera都支持的代码
var cell = documentcreateElement("td").appendChild(document.createTextNode("foo"));
var row = document.createElement("tr").appendChild(cell);
document.getElementById("MyTableBoyd").appendChild(row);
或者:在TR的父节点添加TBODY。(注:在IE中,利用DOM动态添加表格的行时,在TR的上一层必须添加TBODY,否则动态添加之后无法显示出来)
2. 通过JAVASCRIPT设置元素的样式
var spanElement = document.getElementById("myspan");
//spanElement.setAttribute("style","font-weight:bold;color:red"); //IE之外的浏览器
spanElement.style.cssText="font-weight:bold;color:red"; //IE的方法cssText不是标准属性,但得到IE支持
3. IE和FF在添加Option兼容操作,这里直接贴出本人的代码(注:FF支持直接将Option列表以innerHTML形式嵌入到select节点,但IE不支持)
function removeAllOptions(select_id){
var com=document.getElementById(select_id);
var len=com.options.length;
for(var i=0;i<len;i++){
com.remove(0);
}
}
function addOption(e_select,value,text){
var oOption = document.createElement("option");
e_select.options.add(oOption);
if(text!=null){
var text_node=document.createTextNode(text);
oOption.appendChild(text_node);
}
if(value!=null){
oOption.value = value;
}
}
4. 创建输入元素(通用代码)
var button = document.createElement("input");
button.setAttribute("type","button");
document.getElementById("form").appendChild(button);
5. 向输入元素增加事件处理程序(通用代码)
var element = document.getElementById("ee");
element.onclick=function(){todo();};
6. 创建单选按钮
var ration = document.createElement("<input type='radio' name='radionsss' value='checked'>”);//ie
//非ie创建方法
vat radion = document.createElement("input");
radion .setAttribute("type","radio");
radion.setAttribute("name","radionsss");
radion.setAttribute("value","checked");