[英]Hide all DIV exept one called in a link
So, it's a bit akward, but I found a post were somebody explained the question very good. 因此,这有点笨拙,但我发现有人在很好地解释了这个问题。 But I can not find it sadly.
但是我很难过。
So I'll ask here, is there a possibility to hide all div
(with ID's) on a page with JS AND let show one or more div
s via link URL. 所以,我会问这里有没有可以隐藏所有
div
(带编号的)与JS的网页上,让显示一个或多个div
通过链接URL秒。 Let's say like: www.mysite.net/games?id=( _the id of the div_ )
it then should show up on the site and no one of the others. 假设像这样:
www.mysite.net/games?id=( _the id of the div_ )
它应该显示在网站上,而没有其他人出现。
Kinda what I want is that all div
s are hidden and only that shows that is called by the URL ?id=[id of the div]
Kinda我想要的是所有
div
都被隐藏,并且仅显示URL ?id=[id of the div]
调用的那个
Not sure if I got the question right, but is this kinda what you want? 不知道我是否正确回答了这个问题,但这是您想要的吗?
var divs = document.getElementsByTagName("div");
var id = window.location.href.split("=")[1];
divs.map(function(div) {
if(div.id !== id) { div.style['display'] = "none"; }
else { div.style['display'] = "block"; }
});
Sooo. SOOO。 Here is the answer to my own Question:
这是我自己的问题的答案:
The HTML: HTML:
<div hidden id="1">
<h1>Hello Manager</h1>
<p>What would you like to do today?</p>
<button>View Dashboard</button>
</div>
<div hidden id="2">
<h1>Hello employee</h1>
<p>What would you like to do today?</p>
<button>Clock In</button>
</div>
<div hidden id="3">
<h1>Hello Owner</h1>
<p>What would you like to do today?</p>
<button>Test id</button>
</div>
And the JavaScript: 和JavaScript:
// Get query parameter
// Source: https://css-tricks.com/snippets/javascript/get-url-variables/
function getQueryParameter(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return (false);
}
// Capture the `ID` query parameter from the URL.
var id = getQueryParameter('id')
if(id.toLowerCase() === '1') {
$('#1').show();
} else if(id.toLowerCase() === '2') {
$('#2').show();
} else if(id.toLowerCase() === '3') {
$('#3').show();
}
And here is an working example: http://codepen.io/anon/pen/qmpoKr?id=3 这是一个工作示例: http : //codepen.io/anon/pen/qmpoKr?id=3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.