小编use*_*100的帖子

根据if语句显示div

我正在尝试使用此代码隐藏或显示基于 url 参数“方向”的 div 我无法让它工作,想知道是否有更好的方法。示例网址 = http://domain.com?direction=south&season=summer - 谢谢

<head>
<script type="text/javascript">

        function getUrlVars()
        {
            var vars = [], hash;
            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for(var i = 0; i < hashes.length; i++)
            {
                hash = hashes[i].split('=');
                vars.push(hash[0]);
                vars[hash[0]] = hash[1];
            }
            return vars;
        }

        var myVal = getUrlVars()["direction"];


        if(myVal == "north") {

            document.getElementById('north').style.display = 'block'; 
        }else {

            document.getElementById('south').style.display = 'none'; 

        }
        </script>
        </head>

        <body>

        <div id="north">Some text</div>

        <div id="south">Some text</div>

        </body>
Run Code Online (Sandbox Code Playgroud)

javascript

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

javascript ×1