0

我想在不同的 url 中显示/隐藏 div。Div 的显示/隐藏基于 URL。

<div class="top">Top</div>
<div class="bottom">Bottom</div>

如果 www.alldiv.com 两个 div 都应该可见,否则如果 www.bottdiv.com “top div”应该隐藏。

有人可以建议吗?

4

2 回答 2

3

使用主机名你可以做

jQuery

$(function() {
  var host = location.hostname;
  $(".top").toggle(host.indexOf("alldiv")!=-1); // only show on alldiv
});

纯JS

window.onload=function() {
  var host = location.hostname;
  var topDiv = document.getElementsByClassName("top");
  topDiv.style.display=host.indexOf("alldiv")!=-1)?"block":"none";
}

也就是说,如果你想隐藏它,你真的不应该将它发送给客户端,除非你想稍后显示它

于 2012-12-17T15:13:14.200 回答
3
$(function () {
  if(location.hostname == "www.alldiv.com") return;
  else if(location.hostname == "www.bottdiv.com") $(".top").hide();
})

只要您首先导入 jQuery,这应该可以解决问题。

于 2012-12-17T15:16:30.640 回答