我正在学习javascript,我对创建元素感到震惊。它不适用于任何浏览器。这是我的代码:
<body>
<div id="cont">
<script type="text/javascript">
function hi()
{
alert("hi");
}
function replace()
{
var a=document.getElementById('cont');
var b=new Element("form",{name:"pnrForm",action:"\frame.html",method:"post"});
var c=new Element("input",{name:"lccp_trnname",type:"text",id:"lccp_trnname",size:"15",maxlength:"15",alt:"Train Name/Number"});
var d=new Element("input",{type:"image",name:"getIt",src:"button-get-pnr.png",align:"middle"});
b.appendChild(c);
b.appendChild(d);
a.appendChild(b);
}
replace();
hi();
</script>
</div>
</body>
答案 0 :(得分:3)
我相信你的代码期待Mootools出现。试试这个:
<body>
<div id="cont">
<script src="//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script>
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<script type="text/javascript">
function hi()
{
alert("hi");
}
function replace()
{
var a=document.getElementById('cont');
var b=new Element("form",{name:"pnrForm",action:"\frame.html",method:"post"});
var c=new Element("input",{name:"lccp_trnname",type:"text",id:"lccp_trnname",size:"15",maxlength:"15",alt:"Train Name/Number"});
var d=new Element("input",{type:"image",name:"getIt",src:"button-get-pnr.png",align:"middle"});
b.appendChild(c);
b.appendChild(d);
a.appendChild(b);
}
replace();
hi();
</script>
</div>
</body>
答案 1 :(得分:0)
使用document.createElement
(请参阅演示:http://jsfiddle.net/WQaaY/1):
function replace() {
var div = document.getElementById('div');
var form = document.createElement('form');
form.name = "pnrForm";
form.action = "\frame.html"
form.method = "post";
var a = document.createElement('input');
a.id = "lccp_trnname";
a.type = "text";
a.name = "lccp_trnname";
a.alt = "Train Name/Number";
a.size = "15";
a.maxlength = "15";
var b = document.createElement('input');
b.type = "image";
b.name = "getIt";
b.src = "button-get-pnr.png";
b.align = "middle";
form.appendChild(a);
form.appendChild(b);
div.appendChild(form);
}
replace();