如何在URL中包含特定哈希标签时显示文本?
只有使用正确的标识符时才可以显示文本吗?所以,当URL说http://example.com#hello和不可见时,它只是说:http://example.com如何在URL中包含特定哈希标签时显示文本?
(我不是一个讲英语的国,所以我有描述一个困难时期一些文本才可见这一点)。
的,你应该使用JavaScript和检查网址有它与否,然后选择要添加或删除的行为元素..
var textElement = document.getElementById('myTextElement');
if(window.location.hash == '#hello'){
textElement .style.display = 'block';
} else {
textElement .style.display = 'none';
}
希望它有帮助。
if (window.location.hash == "#hello") {
document.getElementById("myElement").style.visibility = "visible"
} else {
document.getElementById("myElement").style.visibility = "hidden"
}
你可能会想要得到的元素,并将其设置为一个变量,而不是调用到DOM两次,但你应该在这里得到的想法。如果您愿意,您也可以使用display: none
而不是可见性属性。
我认为这是可以用纯CSS来实现:
<div id="test">Can you see me?</div>
<style>
#test{opacity:0;}
#test:target{opacity:1}
</style>
为什么downvote? –
问题不在于单击按钮时显示某些内容,而是如何检测URL中特定的内容。 :) –
@kasper christoffersen该按钮是一个重定向到#test ...如果你会看我的答案,你会注意到这一点。 –
'window.location.hash'。如果您遇到困难,请发布您试图解决问题的代码。 –
你可以使用window.location.hash和window.onhashchange来检查值的改变。 – karthick