我正在尝试使用此代码隐藏或显示基于 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) 我想在现有元素之前向容器div添加元素.我尝试过这三种不同的方式而不是
方法1
$('.channel:first',$('#scroll_5')).before(print_nav);
方法2
$('.channel:eq(0)',$('#scroll_5')).before(print_nav);
方法3
$('.channel',$('#scroll_5')).each(function () {
$(this).before(print_nav);
return false;
});
Run Code Online (Sandbox Code Playgroud)
容器div是scroll_5并且具有类的元素channel.我想在顶部添加一个新频道.这样做的正确方法是什么?