function createMsg(){ var oP=document.createElement("p");//创建<p/>节点 var oText=document.createTextNode("Hello world!");//创建文本节点 oP.appendChild(oText);//使用appendChild()方法将oText节点添加到<p/>节点的childNodes列表尾部 //document.body.appendChild(oP);//将oP节点添加到body标签的childNodes列表尾部 document.getElementById("pbody").appendChild(oP);//将oP节点添加到id为pbody的标签中 } 创建,可以参考这个
<label class="highlight" id="aaa"> <input class="radio" type="radio" name="highlight_color" value="8" /> <em style="background: gray;"> </em> </label>
<script>
alert(document.getElementById("aaa").innerHTML=document.getElementById("aaa").innerHTML.toString().replace(/gray/,"#777"));//alert(f2c( document.getElementById("msg").innerHTML ));
</script>
var oP=document.createElement("p");//创建<p/>节点
var oText=document.createTextNode("Hello world!");//创建文本节点
oP.appendChild(oText);//使用appendChild()方法将oText节点添加到<p/>节点的childNodes列表尾部
//document.body.appendChild(oP);//将oP节点添加到body标签的childNodes列表尾部
document.getElementById("pbody").appendChild(oP);//将oP节点添加到id为pbody的标签中
}
创建,可以参考这个
然后
document.getElementById("test").style.background="#777";最好id属性不要跟页面上其它标签的name属性一样,否则在IE下可能会出问题IE的getElementById("test")可能会返回页面上name属性为test的标签
只能用javascript后期添加呢?
oP.style.className="className"
设置一个对象的class在IE下
在火狐下
var oL=document.createElement("label");//创建<label/>节点
var oR=document.createElement("input");//创建<input/>标签
oR.value="9";
oR.className="radio";
oR.type="radio";
oR.name="highlight_color";
oR.style.background="#777";
oL.appendChild(oR);//
//document.body.appendChild(oL);//将oL节点添加到body标签的childNodes列表尾部
document.form1.appendChild(oL);//将oL节点添加到form表单中
}
就是用这种方式创建标签了...
如果是直接在body里的话,使用
document.body.appendChild(oL)就可以将新建节点插入到这些代码的后面
window.onload=function(){createMsg();};
HaTE
等 级:
发表于:2008-01-22 18:25:567楼 得分:0
创建 <p/> 节点是什么意思
--------------------------
那是我随手贴的代码,就是创建<p></p>节点了...
但是学会一个新知识
函数调用最好在onload事件中
//刚才的代码在IE下有些问题,创建好radio之后无法选择,这是修正后的...
function createElement(type, name) {
var element = null;
try{element = document.createElement('<'+type+' name="'+name+'">');}
catch (e) { }
if (!element) {
element = document.createElement(type);
element.name = name;
}
return element;
}
function createMsg(){
var oL=document.createElement("label");//创建<label/>节点
oL.className="highlight";
var oR=createElement("input","highlight_color");//创建<input/>标签
oR.value="9";
oR.className="radio";
oR.type="radio";
oR.style.background="#777";
oL.appendChild(oR);//将radio添加到新建的label标签里
document.form1.appendChild(oL);//将oL节点添加到form表单中,form1改成你用的表单的name名就可以了
}
window.onload=function(){createMsg();};