我正在学习这些东西并试图理解它,因此正在使用它而不是innerhtml.
我的代码:
<script>
function load() {
"use strict";
var t = document.createElement("table"),
tb = document.createElement("tbody"),
tr = document.createElement("tr"),
td = document.createElement("td");
var b = document.createElement("b");
b.appendChild(document.createTextNode(" name!"));
td.appendChild(document.createTextNode("Hello"));
td.appendChild(b);
t.style.width = "100%";
t.style.borderCollapse = 'collapse';
t.border=1;
// note the reverse order of adding child
tr.appendChild(td);
tb.appendChild(tr);
t.appendChild(tb);
// more code to populate table rows and cells
document.getElementById("theBlah").appendChild(t);
alert(t.innerHTML);
}
</script>
</head>
<body onl oad="load()">
问题是,onload()不显示< table>标签,它从< tbody>中显示相反…为什么?
最后,在这段代码中:
var t = document.createElement("table"),
tb = document.createElement("tbody"),
tr = document.createElement("tr"),
td = document.createElement("td");
即使我只为t使用Var关键字,而不是为其他关键字使用Var关键字,为什么即使启用“ use strict”也不会抱怨呢?实际上,如果我在“ tb”,“ tr”,“ td”前面添加“ var”,则会引发错误…
谢谢!
解决方法:
1)innerHTML就是这样.是内在的它不包含元素本身的HTML,而是包含该元素内的所有html.也有一个名为externalHTML的属性,但它不包含在Firefox中.获取外部HTML的唯一跨浏览器方法是将节点包装在新的父节点中,并抓住该节点innerHTML.除非您确定该元素没有兄弟姐妹,否则您不能只是抓住当前父母的innerHTML(必须创建一个新的父母),因为如果这样做,兄弟姐妹将包括在内.
2)您使用逗号分隔可变装饰.这与使用分号并将var放在每行上相同.