Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中激活链接_Javascript_Css - Fatal编程技术网

在Javascript中激活链接

在Javascript中激活链接,javascript,css,Javascript,Css,我对Javascript有一些问题。事实上,我只是那个脚本语言的新手,所以我需要一些帮助。。 问:如何激活此链接: <a href="#box1">something</a> 此链接只是指向index.html文件中的div的链接,因此不需要加载页面。 这是分区 <div id="box1" class="box"> <h3><a name="box1">something</a></h3> </di

我对Javascript有一些问题。事实上,我只是那个脚本语言的新手,所以我需要一些帮助。。 问:如何激活此链接:

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

此链接只是指向index.html文件中的div的链接,因此不需要加载页面。 这是分区

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

某物

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

a.visited{color:red;}

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

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

a:悬停{颜色:红色;}

由于您刚刚起步,我建议您使用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>
使用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中,实现这一点需要付出更多的努力。帮你自己一个忙,不要重新发明轮子-人们已经注意到了这一点,所以用他们的劳动成果让你的生活更轻松。

你说的
激活
是什么意思,例如改变链接的背景颜色,以便向用户显示他/她在哪里。。在css中有一段代码a:active{background:#fff;}但它不起作用,因为没有加载页面在css中没有用,它不起作用,因为点击链接时页面没有重新加载,我需要Javascript$(“a”)。点击(function(){$(this.css('color','#FF0000'))它是一个全局函数,它将更改所有出现的“a”标记的颜色。为什么每个
div
都需要id?@user1807271没有原因。
$(".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
})