如何使用javascript创建xhtml元素 [英] How to create xhtml elements with javascript
问题描述
如何使用javascript动态创建xhtml元素?我想在表格单元格中添加一个复选框。
How can I dynamicaly create xhtml elements with javascript? I want to add a checkbox inside a table cell.
所以我希望代码看起来像这样:
So I want the code to look like this:
<td>
<input type="checkbox" />
</td>
不幸的是,输入元素末尾的斜杠不会被添加,因此不能兼容xhtml。我的代码的结果如下所示:
Unfortunatly the slash at the end of the input element is not added thus making it not xhtml compatible. The result of my code looks like this:
<td>
<input type="checkbox">
</td>
我尝试将innerHTML作为createElement,但两者都没有添加斜杠。
I tried both innerHTML as createElement but both didn't add the slash.
1)
cell.innerHTML = "<input type='checkbox' />";
2)
var checkbox = document.createElement("input");
checkbox.type = "checkbox";
cell.appendChild(checkbox);
有没有办法添加xhtml emement?
Is there a way to add an xhtml emement?
推荐答案
XHTML只是普通DOM中的另一种输入语言。 XHTML语法的区别在于如何*解析**,而不是如何在DOM中表示。所以你的代码很好 - 你正在成功添加元素,它只是在它命中DOM时被翻译。
XHTML is just another input language into the common DOM. What sets XHTML syntax apart is how it is *parsed**, not how it is represented in the DOM. So your code is fine - you ARE adding the element successfully, it's just translated when it hits the DOM.
*除了Internet Explorer,尽管声明XHTML是doctype IE无论如何都会将其解析为HTML。
* except Internet Explorer, which, despite declaring XHTML as the doctype, IE will parse it as HTML anyway.
这篇关于如何使用javascript创建xhtml元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!