2016-06-09 67 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,它重定向。否则它分配值。