我想嵌入并显示 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>
最佳答案
(function () {
var myEmbed = document.createElement("embed");
myEmbed.type="application/x-shockwave-flash";
myEmbed.height='100';
myEmbed.width='100';
…
document.getElementById("test").appendChild(myEmbed);
}());
乍一看,这种方法通常显得不必要的嘈杂和毫无值(value),但它与 HTML 的自然语法集成得更加合理。如果您希望能够将 HTML 直接写入文档,您可能会更喜欢 jQuery 这样的框架,它允许使用如下语法:
$("<embed src='…'/>")
但是,如果您只是执行所有这些操作来嵌入 Flash,请使用 swfobject .
关于Javascript document.write 和 DOM element.AppendChild(text) 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7830820/