本文介绍了在Javascript中启用链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Javascript的问题。事实上,我只是新手的脚本语言,所以我需要一些帮助..
Q:如何使这个链接有效:

I have some problems with Javascript. In fact, I'm just newbie in that script language so I need some help..Q: how to make this link active:

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

此链接只是链接到位于index.html文件中的div,因此没有加载页面。
and here is the div

this link is just link to the div which is located in index.html file, so there is no loading of page.and here is the div

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


推荐答案

建议您使用jQuery等库。所以,如果你的HTML是这样:

Since you're just starting out, I'd suggest you use a library such as jQuery. So, if your HTML is like this:

<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>

您有一个CSS类 youarehere

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

使用jQuery,你可以写如下代码:

With jQuery you could write something along the lines of:

$(".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中,需要更多的努力来实现。做自己一个好处,不要重复轮子 - 人们已经照顾了这一点,所以使用他们的劳动的产品,让你的生活更容易。

In plain JS, it takes a bit more effort to achieve this. Do yourself a favor and don't reinvent the wheel - people have already taken care of this, so use the product of their labor to make your life easier.

这篇关于在Javascript中启用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 21:22
查看更多