Javascript条件 - 不隐藏ID

时间:2015-02-24 19:31:23

标签: javascript jquery

我试图隐藏ID"隐藏主页"并且它的整体工作,除了我想要在指定的URL(http://wgzrv.ndxva.servertrust.com/login.asp)隐藏它的第二个条件。我错过了什么吗?

<script type="text/javascript">    
    $(window).resize(function(){ 
        function showMyDiv() {
        if (window.location.href == "http://wgzrv.ndxva.servertrust.com") && (document.documentElement.clientWidth > 992) { 
        document.getElementById("hide-homepage").style.display="none";
        } else if (window.location.href == "http://wgzrv.ndxva.servertrust.com/login.asp") {
        document.getElementById("hide-homepage").style.display="none";
        } else if (document.documentElement.clientWidth < 992) {
        document.getElementById("hide-homepage").style.display="none";
        } else {
        document.getElementById("hide-homepage").style.display="block";
            }
        } 
    });
    </script>

2 个答案:

答案 0 :(得分:1)

    if (window.location.href == "http://wgzrv.ndxva.servertrust.com") && (document.documentElement.clientWidth > 992) { 

应该是

    if (window.location.href == "http://wgzrv.ndxva.servertrust.com" && document.documentElement.clientWidth > 992) { 

答案 1 :(得分:1)

使用indexOf()

尝试代替==
<script type="text/javascript">    
$(window).resize(function(){ 
    function showMyDiv() {
    if (window.location.href == "http://wgzrv.ndxva.servertrust.com") && (document.documentElement.clientWidth > 992) { 
    document.getElementById("hide-homepage").style.display="none";
    } else if (window.location.href.indexOf("http://wgzrv.ndxva.servertrust.com/login.asp") > -1) {
    document.getElementById("hide-homepage").style.display="none";
    } else if (document.documentElement.clientWidth < 992) {
    document.getElementById("hide-homepage").style.display="none";
    } else {
    document.getElementById("hide-homepage").style.display="block";
        }
    } 
});
</script>

编辑(我删除了内部函数,第一次没看到它):

<script type="text/javascript">    
$(window).resize(function(){ 
    if (window.location.href == "http://wgzrv.ndxva.servertrust.com") && (document.documentElement.clientWidth > 992) { 
    document.getElementById("hide-homepage").style.display="none";
    } else if (window.location.href.indexOf("http://wgzrv.ndxva.servertrust.com/login.asp") > -1) {
    document.getElementById("hide-homepage").style.display="none";
    } else if (document.documentElement.clientWidth < 992) {
    document.getElementById("hide-homepage").style.display="none";
    } else {
    document.getElementById("hide-homepage").style.display="block";
        }
});
</script>