在 Javascript 中激活链接

Making links active in Javascript

本文关键字:链接 激活 Javascript      更新时间:2023-09-26

我在使用Javascript时遇到了一些问题。事实上,我只是那种脚本语言的新手,所以我需要一些帮助。问:如何激活此链接:

<a href="#box1">something</a>

此链接只是指向位于 index.html 文件中的div 的链接,因此没有加载页面。这是div

<div id="box1" class="box">
<h3><a name="box1">something</a></h3>
</div>

由于你刚刚起步,我建议你使用像jQuery这样的库。所以,如果你的HTML是这样的:

<div id="box1" class="box">
<h3><a name="box1">something</a></h3>
</div>
<div id="box2" class="box">
<h3><a name="box2">something</a></h3>
</div>
<div id="box3" class="box">
<h3><a name="box3">something</a></h3>
</div>

你有一个名为 youarehere 的 CSS 类:

.youarehere { color:white; background:green; }

使用jQuery,你可以写一些类似的东西:

$(".box > a").click(function() {             // when clicking any of these links
    $(".box > a").removeClass("youarehere"); // remove highlight from all links
    $(this).addClass("youarehere");          // add highlight to clicked link
})

在普通JS中,要实现这一目标需要更多的努力。帮自己一个忙,不要重新发明轮子——人们已经照顾好了这一点,所以使用他们的劳动产品让你的生活更轻松。

a:active 表示当您单击链接时,CSS 属性将应用于链接,而不是使用 a:active use

a.visited{color:red;}

要在鼠标悬停时更改链接文本颜色,请使用以下 css:

<style type="text/css">
        a:hover{color:Red;}
    </style>