2016-06-09 23 views
-1

最近我遇到了一個問題。我想在我的博客模板中添加此代碼。我已經給它取代'display:none; css'選項,用我的主頁功能替換信用網址。但if (footer === null) {window.location = 'http://google.com';}不起作用。爲什麼?有什麼問題嗎?如果他們刪除id='mycreditlink'屬性,我想讓客戶端博客重定向。我想在沒有jQuery的情況下做到這一點。如何給它檢查div id功能?

<!DOCTYPE html> 
<html> 
    <head> 

     <script type="text/javascript"> 

      function creditprotection(){ 

       var url = ('http://grplusbd.net'); 
       var style = ('display: inline; visibility: true;'); 
       var footer = document.getElementById('mycreditlink'); 

       footer.href= url; 
       footer.style = style; 

       if (footer === null) { 
       window.location = 'http://google.com'; 
       } 
      } 

      window.onload = function(){creditprotection();}; 

     </script> 



    </head> 


    <body> 

      Powered By <a href='http://google.com' id='mycreditlink'> My Site </a> 

    </body> 
</html> 
+0

這是不可能永遠到達那裏。如果'footer === null'爲true,那麼代碼已經崩潰,因爲它不能在空引用上設置'href'或'style'。 –

+0

感謝您的幫助,現在它正在工作......我已經放置了footer.href = url;和footer.style = style;在if(footer === null)之後的else {}中, –

回答

1

<head> 

    <script type="text/javascript"> 

     function creditprotection(){ 

      var url = ('http://grplusbd.net'); 

      var style = ('display: inline; visibility: true;'); 

      var footer = document.getElementById('mycreditlink'); 

      if (footer == null) { 

       footer.href= url; 

       footer.style = style; 

       window.location = 'http://google.com'; 
      } 
     }window.onload = function(){creditprotection();}; 

    </script> 
</head> 

<body> 

Powered By <a href='http://google.com' id='mycreditlink'> My Site</a> 

</body> 

0

你可以改變你的函數是這樣的:

function creditprotection() { 
    var url = 'http://grplusbd.net'; 
    var style = 'display: inline; visibility: true;'; 
    var footer = document.getElementById('mycreditlink'); 

    if (footer === null) { 
    window.location = 'https://google.com'; 
    } 

    footer.href = url; 
    footer.style = style; 
} 

footer沒有設置,它不具有的特性hrefstyle。那是你遇到錯誤的時候。這可以通過在將值分配給頁腳屬性之前檢查何時footer === null來防止。如果footer === null,它重定向。否則它分配值。