javascript
  • getelementbyid
  • appendchild
  • createelement
  • 2011-12-01 93 views 0 likes 
    0

    我想创建一个使用JavaScript的iframe,然后将其插入两个部门都有id“fblike”。我试图使用下面的代码,但它没有奏效。Javascript创建元素在两个部门

    <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> 
    

    我知道在代码中一定有一些错误,因为我对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> 
    
    +0

    欢迎堆栈溢出!现在,这已经解决了问题,您应该接受您认为回答您的问题的任何答案。 – Yahel

    回答

    2

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

    1

    的函数被调用document.getElementById,它返回一个元素,因为你永远不能有相同的ID

    多个元件,以改变

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

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

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

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

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

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

    0

    其实,你想插入两个iframe到两个div,对吧?这里是一个会做你想要什么样的代码(不能说是最佳的,因为你有重复的ID - 如上面的人说,你最好让两个不同的ID如果可能的话):

    <script type="text/javascript"> 
        (function(){ 
        var list = document.getElementsByTagName('div'), i, fb; 
        for (i = 0; i < list.length; i++) { 
         if (list[i].id == 'fblike') { 
          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'; 
          list[i].appendChild(fb); 
         } 
        } 
        })(); 
    </script> 
    
    相关问题