Javascript 当URL中有特定的标签时,如何使文本显示?

Javascript 当URL中有特定的标签时,如何使文本显示?,javascript,html,hashtag,appearance,Javascript,Html,Hashtag,Appearance,如果只使用正确的id,是否可以显示文本?因此,某些文本仅在URL显示时可见,而在仅显示时不可见 (我不是英语国家的人,所以我很难描述这一点。)为此,您应该使用JavaScript并检查URL是否有,然后选择要添加或删除行为的元素 var textElement = document.getElementById('myTextElement'); if(window.location.hash == '#hello'){ textElement .style.display = 'blo

如果只使用正确的id,是否可以显示文本?因此,某些文本仅在URL显示时可见,而在仅显示时不可见


(我不是英语国家的人,所以我很难描述这一点。)

为此,您应该使用JavaScript并检查URL是否有,然后选择要添加或删除行为的元素

var textElement = document.getElementById('myTextElement');
if(window.location.hash == '#hello'){
    textElement .style.display = 'block';
} else {
    textElement .style.display = 'none';
}

希望对您有所帮助。

为此,您应该使用JavaScript,检查URL是否有,然后选择要添加或删除行为的元素

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"
}
希望能有帮助

if (window.location.hash == "#hello") {
    document.getElementById("myElement").style.visibility = "visible"
} else {
    document.getElementById("myElement").style.visibility = "hidden"
}
您可能希望获取元素并将其设置为变量,而不是两次都调用DOM,但您应该在这里了解这一点。如果需要,还可以使用
display:none
而不是visibility属性


您可能希望获取元素并将其设置为变量,而不是两次都调用DOM,但您应该在这里了解这一点。如果愿意,也可以使用
display:none
而不是visibility属性。

我认为这可以通过纯css实现:

<div id="test">Can you see me?</div>

<style>
    #test{opacity:0;} 
    #test:target{opacity:1}
</style>
你能看见我吗?
#测试{不透明度:0;}
#测试:目标{不透明度:1}

我认为这可以通过纯css实现:

<div id="test">Can you see me?</div>

<style>
    #test{opacity:0;} 
    #test:target{opacity:1}
</style>
你能看见我吗?
#测试{不透明度:0;}
#测试:目标{不透明度:1}

窗口.位置.散列
。如果您还有其他困难,请发布您试图解决问题的代码。您可以使用window.location.hash和window.onhashchange检查值何时更改。
window.location.hash
。如果您还有其他困难,请发布您试图解决问题的代码。您可以使用window.location.hash和window.onhashchange检查值何时更改。您是否有变量hello?编辑后,它仍将进入else。哈希将有一个前导#是否有一个变量hello?编辑后,它仍将进入else。哈希将有一个前导#问题不是在单击按钮时使某些内容出现,而是如何检测URL中的特定内容。:)@kasper christoffersen该按钮重定向到#测试。。。如果你读过我的答案,你会注意到这一点。问题不在于单击按钮时出现什么,而是如何检测URL中的特定内容。:)@kasper christoffersen该按钮重定向到#测试。。。如果你读了我的答案,你会注意到的。