如何在URL上使用哈希时打开隐藏的div? [英] How to open a hidden div when a hash is on the URL?
问题描述
我正在使用这个javascript代码在我的网站上有一对显示/隐藏切换的div:
I'm using this javascript code to have a couple "show/hide" toggled divs on my site:
<script language="javascript">
function toggledean() {
var ele = document.getElementById("toggleTextdean");
var text = document.getElementById("displayTextdean");
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "Show more";
}
else {
ele.style.display = "block";
text.innerHTML = "Hide";
}
}
</script>
在加载具有特定页面的页面时,我应该添加什么来显示div?哈希添加到URL?
What should I add to this code to have the div be displayed upon loading the page with a specific #hash added to the URL?
非常感谢。
推荐答案
这不是您想要的javascript答案,但您可以尝试使用:target
伪选择器。例如,
This is not the javascript answer that you want, but you could try playing around with the :target
pseudo selector. For instance,
<!-- HTML -->
<div id="foo">show this with #foo.</div>
<div id="bar">#bar shows this.</div>
<style type="text/css">
div {display: none}
:target {display: block}
</style>
示例: http://jsfiddle.net/ZAHns/4/ (感谢Jared添加锚点的想法)。
Example: http://jsfiddle.net/ZAHns/4/ (thanks to Jared for the idea of adding the anchors).
取决于关于你想要做什么,这可能会有效,但是要考虑你的要求。
Depending on what you are trying to do, this could possibly work, but think your requirements through.
注意:采取这种反应有一大堆 - 不要使用它。
Note: Take this response with a HUGE grain of salt -- don't use it.
要回答实际问题,请使用以下内容确定是否存在哈希:
To answer the actual question, use the following to determine if the hash is present:
var in_hash = location.hash.slice(1) === what_you_are_looking_for;
if (in_hash) ? /* IN HASH */ : /* NOT IN HASH */;
这篇关于如何在URL上使用哈希时打开隐藏的div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!