使用 javascript createElement 创建 <br />?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4654265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a <br /> with javascript createElement?
提问by Phil
I need to create a <br />
tag dynamically with javascript.
我需要<br />
使用 javascript 动态创建一个标签。
var br = document.createElement('br');
And
和
var br = document.createElement('<br />');
doesn't work
不起作用
The first option urrently creates a <br>
tag on the page which doesn't pass XHTML standards, is there anyway around this?
第一个选项目前<br>
在页面上创建了一个不通过 XHTML 标准的标签,是否有解决办法?
采纳答案by jball
The first method does not need to pass XHTML standards - you're confusing markup with manipulating the DOM.
第一种方法不需要通过 XHTML 标准 - 您将标记与操作 DOM 混淆了。
回答by cdhowie
The first option will work and has nothing to do with XHTML since the DOM operations are performed afterparsing of the document, and therefore there is no XHTML/HTML standard to be compliant to at that point. As long as you are not trying to output the HTML to a string, this approach will work just fine.
第一个选项将起作用并且与 XHTML 无关,因为 DOM 操作是在解析文档之后执行的,因此此时没有符合 XHTML/HTML 标准。只要您不尝试将 HTML 输出为字符串,这种方法就可以正常工作。
回答by Nagri
A Simple Script of HTML and JavaScript to add br tag dynamically in the page.
一个简单的 HTML 和 JavaScript 脚本,用于在页面中动态添加 br 标签。
<SCRIPT language="javascript">
function add() {
//Create an input type dynamically.
var br = document.createElement("br");
var foo = document.getElementById("fooBar");
foo.appendChild(br);
}
</SCRIPT>
<INPUT type="button" value="Add" onclick="add()"/>
<span id="fooBar"> </span>
This text will go down every time you click on add button
回答by kevinji
In reality, your code will still validate if your JavaScript doesn't exactly validate, because the validator only parses the processed HTML code, not the dynamic code created by JavaScript. If you cared, you could also use an innerHTML
trick, or a document.createTextNode('<br />');
if you wanted to force the XHTML version, but it is not recommended. document.createElement('br');
works perfectly fine.
实际上,如果您的 JavaScript 没有完全验证,您的代码仍然会进行验证,因为验证器只解析处理过的 HTML 代码,而不是 JavaScript 创建的动态代码。如果你关心,你也可以使用一个innerHTML
技巧,或者document.createTextNode('<br />');
如果你想强制使用 XHTML 版本,但不推荐这样做。document.createElement('br');
工作得很好。
回答by RobG
This question has nothing whatever to do with markup. The argument passed to createElementis an element name, it is not a fragment of markup or anything else, it isn't XML, or HTML, or XHTML, or SGML or any form of markup.
这个问题与标记无关。传递给createElement的参数是元素名称,它不是标记片段或其他任何东西,也不是 XML、HTML、XHTML、SGML 或任何形式的标记。
The createElementmethod returns a DOM object, there is no markup and never was.
的的createElement方法返回一个DOM对象,没有标记,绝不是。
Note that the markup passed to the browser is used to create a document object, from that point onward, the markup is irrelevant.
请注意,传递给浏览器的标记用于创建文档对象,从那时起,标记就无关紧要了。