javascript
  • getelementbyid
  • appendchild
  • createelement
  • 2011-12-01 82 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> 
    
    相關問題