[英]creating table via document.onload won't function
I'm wondering as to why the following ceases to function, for whatever peculiar reason:我想知道为什么以下内容停止为 function,无论出于何种特殊原因:
document.onload=function() {
table = document.createElement("table");
table.setAttribute("border", "1");
document.body.appendChild(table);
};
Any help would be greatly appreciated!任何帮助将不胜感激!
You all have my sincere thanks for offering your solutions: :D衷心感谢你们提供解决方案::D
Are you sure you don't mean window.onload
?你确定你不是指
window.onload
吗?
window.onload=function() {
table = document.createElement("table");
table.setAttribute("border", "1");
document.body.appendChild(table);
};
http://jsfiddle.net/rzfuG/ http://jsfiddle.net/rzfuG/
EDIT编辑
With some extra html created for demonstration purposes:为演示目的创建了一些额外的 html:
window.onload=function() {
table = document.createElement("table");
table.setAttribute("border", "1");
document.body.appendChild(table);
tbody = document.createElement('tbody');
tr = document.createElement('tr');
td = document.createElement('td');
td.innerHTML = "test";
table.appendChild(tbody);
tbody.appendChild(tr);
tr.appendChild(td);
};
http://jsfiddle.net/rzfuG/1/ http://jsfiddle.net/rzfuG/1/
EDIT 2编辑 2
Now, if you're thinking of the body tag's onload attribute, you could do:现在,如果您正在考虑 body 标签的 onload 属性,您可以这样做:
<body onload="my_onload();"></body>
function my_onload() {
table = document.createElement("table");
table.setAttribute("border", "1");
document.body.appendChild(table);
tbody = document.createElement('tbody');
tr = document.createElement('tr');
td = document.createElement('td');
td.innerHTML = "test";
table.appendChild(tbody);
tbody.appendChild(tr);
tr.appendChild(td);
};
http://jsfiddle.net/rzfuG/2/ http://jsfiddle.net/rzfuG/2/
Note, however, this is the same as window.onload
.但是请注意,这与
window.onload
相同。 In fact, you can't do both window.onload=...
and <body onload="...
, since the body onload will override the JS and only one will run.事实上,你不能同时做
window.onload=...
和<body onload="...
,因为 body onload 会覆盖 JS,只有一个会运行。
See http://jsfiddle.net/rzfuG/3/见http://jsfiddle.net/rzfuG/3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.