Javascript将创建元素分为两部分

Javascript将创建元素分为两部分,javascript,getelementbyid,appendchild,createelement,Javascript,Getelementbyid,Appendchild,Createelement,我想使用javascript创建一个iframe,然后将其插入两个分区中,两个分区的id都是“fblike”。我试图使用下面的代码,但它不起作用 <script type="text/javascript"> (function(){ var fb = document.createElement('iframe'); fb.src = 'http://www.facebook.com/plugins/like.php?href='+encodeURICompon

我想使用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)+'&amp;locale=en_US&amp;send=false&amp;layout=button_count&amp;width=90&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=verdana&amp;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>

(功能(){
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='否';
fb.frameborder='0';
fb.style='边框:无;溢出:隐藏;宽度:90px;高度:21px;';
fb.allowTransparency=‘无’;
document.getElementsById('fblike')[0].appendChild(fb);
})();
我知道代码中一定有一些错误,因为我对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>

(功能(){
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='边框:无;溢出:隐藏;宽度:90px;高度:21px;';
fb.frameBorder='0';
fb.scrolling='否';
fb.allowTransparency=‘真’;
document.getElementById('fbOver').appendChild(fb.cloneNode(true));
document.getElementById('fBbellow').appendChild(fb.cloneNode(true));
})();

该函数称为
document.getElementById
,它返回一个元素,因为不能有多个具有相同id的元素

所以改变

document.getElementsById('fblike')[0].appendChild(fb);


函数名为
document.getElementById
,它返回一个元素,因为不能有多个具有相同id的元素

所以改变

document.getElementsById('fblike')[0].appendChild(fb);


同一页上的
id
属性值不应相等。首先修复这个问题,否则
document.getElementById()
永远不会正常工作(它只返回一个元素),并且
document.getElementsById()
(带有
s
)不存在。

您不应该在同一页上有一个值相等的
id
属性。首先修复这个问题,否则
document.getElementById()
永远不会正常工作(它只返回一个元素),并且
document.getElementsById()
(带有
s
)不存在。

首先,您有一个类型。它的
getElementById
,因为在任何给定页面上只有一个元素可以具有ID

其次,因为只有一个,所以不需要
[0]

document.getElementById('fblike').appendChild(fb);

但是,否则,我们需要更多的细节来了解这是否是您唯一的问题。

首先,您有一个类型。它的
getElementById
,因为在任何给定页面上只有一个元素可以具有ID

其次,因为只有一个,所以不需要
[0]

document.getElementById('fblike').appendChild(fb);

但是,否则,我们需要更多的细节来了解这是否是您唯一的问题。

实际上,您希望在两个div中插入两个iframe,对吗?下面是您想要的代码(不能说它是最佳的,因为您有重复的ID——正如上面的人所说,如果可能,您最好创建两个不同的ID):


(功能(){
var list=document.getElementsByTagName('div'),i,fb;
对于(i=0;i
实际上,您想在两个div中插入两个iframe,对吗?下面是您想要的代码(不能说它是最佳的,因为您有重复的ID——正如上面的人所说,如果可能,您最好创建两个不同的ID):


(功能(){
var list=document.getElementsByTagName('div'),i,fb;
对于(i=0;i
欢迎来到StackOverflow!现在问题已经解决了,您应该接受您认为可以回答您问题的任何答案。欢迎使用StackOverflow!现在问题解决了,你应该接受你认为能回答你问题的答案。