Javascript document.write 和 DOM element.AppendChild(text) 之间的区别
我想嵌入并显示 Flash 文件。它与 document.write 配合得很好,但是当我尝试使用 AppendChild(text) 时,它不显示 flash,而是显示纯文本。使用什么 DOM 方法来代替?
<html>
<head>
<script>
function addText(text,elementId){
if (document.createTextNode){
var mytextNode=document.createTextNode(text)
document.getElementById(elementId).appendChild(mytextNode)
}
}
</script>
</head>
<body>
<div id="test">
</div>
<script>
addText("<embed height='100' width='100' type='application/x-shockwave-flash' allowscriptaccess='always' wmode='transparent' quality='high' swliveconnect='true' name='test' id='test' src='test.swf'>",'test');
</script>
</body></html>
I want to embed and show a flash file. It works fine with document.write but when I tried with AppendChild(text) it doesn't show flash but pure text instead. What DOM method to use instead ?
<html>
<head>
<script>
function addText(text,elementId){
if (document.createTextNode){
var mytextNode=document.createTextNode(text)
document.getElementById(elementId).appendChild(mytextNode)
}
}
</script>
</head>
<body>
<div id="test">
</div>
<script>
addText("<embed height='100' width='100' type='application/x-shockwave-flash' allowscriptaccess='always' wmode='transparent' quality='high' swliveconnect='true' name='test' id='test' src='test.swf'>",'test');
</script>
</body></html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
乍一看,这种方法常常显得不必要的嘈杂和毫无价值,但它与 HTML 的自然语法集成得更加合理。如果您希望能够将 HTML 直接写入文档,您可能会更喜欢像 jQuery 这样的框架,它允许使用以下语法:
但是,如果您只是执行所有这些操作来嵌入 Flash,请使用 swfobject。
At first glance such an approach can often seem unnecessarily noisy and worthless, but it integrates much more sanely with the natural syntax of HTML. If you want to be able to write HTML directly into your document, you might be happier with a framework like jQuery, which allows syntax like:
However, if you're just doing all this to embed Flash, use swfobject.
因为您正在创建文本节点,而不是序列化的 HTML。
您可以将函数更改为...
jsFiddle。
Because you are creating a text node, not serialised HTML.
You could change the function to...
jsFiddle.