0

Let me try to make myself clear: I have a menu that, when you click on a link, it jumps down to the content in that same page. In other words, it's just an link that anchors to the content. Could I possibly change the color of, let's say, the content's h1 tag after clicking on the link?

I also thought of a different way of solving, but I'm not quite sure: could I change the color of a H1 tag depending on its position as you scroll down a page?

Cheers!

4

3 回答 3

2

简单的:

h1:target {color: red;}
于 2012-04-27T14:58:31.023 回答
0

CSS3 :target 伪选择器会做你想做的事,但如果浏览器支持是个问题,你可以这样做:

jQuery

$('nav li a').click(function (e) {
    var targ = $(this).attr('href');
    $('html, body').scrollTop($("'" + targ + "'").offset());
    $("'" + targ + "'").css('color','red');
    e.preventDefault();
});

HTML:

<nav>
    <ul>
        <li><a href="#someElement">Click here!</a></li>
    </ul>
</nav>
于 2012-04-27T15:03:28.347 回答
0

是的,您可以在 CSS 中使用 :target 伪选择器。

http://jsfiddle.net/8rHvy/

有关当前浏览器兼容性的信息,请参见此处: http ://www.quirksmode.org/css/contents.html

于 2012-04-27T14:58:50.040 回答