【发布时间】:2012-01-10 06:44:40
【问题描述】:
我想使用 javascript 创建一个 iframe,然后将其插入到 id 为“fblike”的两个部门中。我尝试使用以下代码,但没有成功。
<script type="text/javascript">
(function(){
var fb = document.createElement('iframe');
fb.src = 'http://www.facebook.com/plugins/like.php?href='+encodeURIComponent(location.href)+'&locale=en_US&send=false&layout=button_count&width=90&show_faces=false&action=like&colorscheme=light&font=verdana&height=21';
fb.scrolling = 'no';
fb.frameborder = '0';
fb.style = 'border:none; overflow:hidden; width:90px; height:21px;';
fb.allowTransparency = 'none';
document.getElementsById('fblike')[0].appendChild(fb);
})();
</script>
我知道代码中一定有一些错误,因为我对 javascript 知之甚少。任何人请帮助我! 谢谢
更新:感谢您的帮助 :) 我更新了代码以消除错误。现在下面的代码对我有用:
<script type="text/javascript">
(function(){
var fb = document.createElement('iframe');
fb.src = 'http://www.facebook.com/plugins/like.php?href='+encodeURIComponent(location.href)+'&locale=en_US&send=false&layout=button_count&width=90&show_faces=false&action=like&colorscheme=light&font=verdana&height=21';
fb.style.cssText = 'border:none; overflow:hidden; width:90px; height:21px;';
fb.frameBorder = '0';
fb.scrolling = 'no';
fb.allowTransparency = 'true';
document.getElementById('fbabove').appendChild(fb.cloneNode(true));
document.getElementById('fbbelow').appendChild(fb.cloneNode(true));
})();
</script>
【问题讨论】:
-
欢迎来到 StackOverflow!既然这已经解决了问题,您应该接受您认为回答了您的问题的任何答案。
标签: javascript getelementbyid appendchild createelement