Html 页面首次加载时如何将活动链接设置为默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2147844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:00:39  来源:igfitidea点击:

How to set a active link as default when page first time load

htmlcsshyperlink

提问by Amra

I need some help to set a link as active as default when the page load the first time.

当页面第一次加载时,我需要一些帮助来将链接设置为默认活动状态。

<style type="text/css">
a{
color:black;
}
a:hover{
color:white;
}
a:active{
color:blue;
}
</style>


<div>
<!--I want this fisrt link to be set as active by default-->
<a href="#"/>
<!--I want this one as normal-->
<a href="#"/>
</div>

采纳答案by Dominic Rodger

If you can change your markup to this:

如果您可以将标记更改为:

<div>
<!--I want this first link to be set as active by default-->
<a href="#" id="focusmeplease"/>
<!--I want this one as normal-->
<a href="#"/>
</div>

Then you can use this JavaScript:

然后你可以使用这个 JavaScript:

document.getElementById('focusmeplease').focus();

Attach that JavaScript to page load however you like(I like this way, unless you're using jQuery, in which case use $(document).ready()).

将该 JavaScript 附加到您喜欢的页面加载(我喜欢这种方式,除非您使用 jQuery,在这种情况下使用$(document).ready())。

回答by Davide Muzzarelli

  1. Mark the "a" tags with a class (like "focus").
  2. Set all the active "a" tags in the class "focus" with your preferred look.
  1. 用一个类(如“焦点”)标记“a”标签。
  2. 使用您喜欢的外观设置“focus”类中的所有活动“a”标签。

<style type="text/css">
a
{color:black;}
a:hover
{color:white;}
a.focus:link, a.focus:visited
{color:blue;}
</style>

<div>
<a href="#" class="focus">This link is active by default.</a>
<a href="#">This is a normal link.</a>
</div>

Final note: I have also corrected the "a" tag because was wrong.

最后说明:我还更正了“a”标签,因为它是错误的。

回答by Tor Valamo

a{
  color:black;
}
a:hover {
  color:white;
}
a:active, div a:first-child {
  color:blue;
}

Supported by most recent browsers, but not much more than that.

受最新浏览器支持,但仅此而已。