使用createElement动态创建HTML对象
2008-05-23 16:13
681 查看
1.创建链接
<script language="javascript">
var o = document.body;
//创建链接
function createA(url,text)
{
var a = document.createElement("a");
a.href = url;
a.innerHTML = text;
a.style.color = "red";
o.appendChild(a);
}
createA("http://www.webjx.com/","网页教学网");
</script>
2.创建DIV
<script language="javascript">
var o = document.body;
//创建DIV
function createDIV(text)
{
var div = document.createElement("div");
div.innerHTML = text;
o.appendChild(div);
}
createDIV("网页教学网:http://www.webjx.com/");
Webjx.Com
</script>
3.创建表单项
<script language="javascript">
var o = document.body;
//创建表单项
function createInput(sType,sValue)
{
var input = document.createElement("input");
input.type = sType;
input.value = sValue;
o.appendChild(input);
}
createInput("button","ooo");
</script>
4.创建表格
<script language="javascript">
var o = document.body;
//创建表格
function createTable(w,h,r,c)
{
var table = document.createElement("table");
var tbody = document.createElement("tbody");
table.width = w;
table.height = h;
table.border = 1;
for(var i=1;i<=r;i++)
{
var tr = document.createElement("tr");
for(var j=1;j<=c;j++)
{
var td = document.createElement("td");
td.innerHTML = i + "" + j;
//td.appendChild(document.createTextNode(i + "" + j));
td.style.color = "#FF0000";
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
o.appendChild(table);
}
createTable(270,270,9,9);
</script>
注意:一定要有tbody,否则IE下不能创建表格,FF下可以!
<script language="javascript">
var o = document.body;
//创建链接
function createA(url,text)
{
var a = document.createElement("a");
a.href = url;
a.innerHTML = text;
a.style.color = "red";
o.appendChild(a);
}
createA("http://www.webjx.com/","网页教学网");
</script>
2.创建DIV
<script language="javascript">
var o = document.body;
//创建DIV
function createDIV(text)
{
var div = document.createElement("div");
div.innerHTML = text;
o.appendChild(div);
}
createDIV("网页教学网:http://www.webjx.com/");
Webjx.Com
</script>
3.创建表单项
<script language="javascript">
var o = document.body;
//创建表单项
function createInput(sType,sValue)
{
var input = document.createElement("input");
input.type = sType;
input.value = sValue;
o.appendChild(input);
}
createInput("button","ooo");
</script>
4.创建表格
<script language="javascript">
var o = document.body;
//创建表格
function createTable(w,h,r,c)
{
var table = document.createElement("table");
var tbody = document.createElement("tbody");
table.width = w;
table.height = h;
table.border = 1;
for(var i=1;i<=r;i++)
{
var tr = document.createElement("tr");
for(var j=1;j<=c;j++)
{
var td = document.createElement("td");
td.innerHTML = i + "" + j;
//td.appendChild(document.createTextNode(i + "" + j));
td.style.color = "#FF0000";
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
o.appendChild(table);
}
createTable(270,270,9,9);
</script>
注意:一定要有tbody,否则IE下不能创建表格,FF下可以!
相关文章推荐
- javascript---使用createElement动态创建HTML对象.窗体传值
- javascript---使用createElement动态创建HTML对象
- javascript---使用createElement动态创建HTML对象.窗体传值
- 使用createElement动态创建HTML对象
- javascript---使用createElement动态创建HTML对象.窗体传值
- javascript---使用createElement动态创建HTML对象
- createElement动态创建HTML对象脚本代码
- createElement动态创建HTML对象
- createElement动态创建HTML对象脚本代码
- 在js中使用createElement创建HTML对象和元素
- 在js中使用createElement创建HTML对象和元素
- 在js中使用createElement创建HTML对象和元素
- 在js中使用createElement创建HTML对象和元素
- 在js中使用createElement创建HTML对象和元素 (转)
- 在js中使用createElement创建HTML对象和元素
- 关于使用动态代理创建代理对象是报错 java.lang.ClassCastException: $Proxy0 cannot be cast to 的解决办法
- 从HTML创建一个BeautifulSoup对象并使用select()方法寻找元素
- JS中定义对象方式五: 使用动态原型方式创建对象
- PowerShell小技巧之使用New-Module命令动态创建对象
- php使用变量动态创建类的对象用法示例