最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding CSS when you access a URL with a specific ID in it - Stack Overflow

programmeradmin3浏览0评论

I have a page with multiple headers, which all have there own ID. The idea is when you click on a specific link, it takes you to the page with the headers and will scroll down to the appropriate header. However on some of the pages, there isn't enough content for the page to scroll down and for the header to site at the top of the page. So I want to highlight the header that the content references, so it's clear which content the link represents.

So the link would be something like




Then once you've accessed the URL it would drop you down to on the page. But the page isn't long enough for the header to sit at the top of the page. So i want to be able to add a background colour to the header.

Updated HTML:

<h3> Header One </h3>
<a name="headerOne"></a>

<p>Content here</p>

I'm persuming some Jquery would need doing but not sure where to start with it. I was thinking of detecting the URL and then doing an if else statement, but this seems like a long winded approach.

Any help would be much appreciated.

Thanks

I have a page with multiple headers, which all have there own ID. The idea is when you click on a specific link, it takes you to the page with the headers and will scroll down to the appropriate header. However on some of the pages, there isn't enough content for the page to scroll down and for the header to site at the top of the page. So I want to highlight the header that the content references, so it's clear which content the link represents.

So the link would be something like

http://website./test#headerOne

http://website./test#headerTwo

Then once you've accessed the URL http://website./test#headerOne it would drop you down to on the page. But the page isn't long enough for the header to sit at the top of the page. So i want to be able to add a background colour to the header.

Updated HTML:

<h3> Header One </h3>
<a name="headerOne"></a>

<p>Content here</p>

I'm persuming some Jquery would need doing but not sure where to start with it. I was thinking of detecting the URL and then doing an if else statement, but this seems like a long winded approach.

Any help would be much appreciated.

Thanks

Share Improve this question edited Jun 14, 2013 at 11:16 JDavies asked Jun 14, 2013 at 10:57 JDaviesJDavies 2,7707 gold badges36 silver badges55 bronze badges 1
  • 1 dynamically add css via jquery api.jquery./css oru use add class/remove class methods api.jquery./addClass – Okky Commented Jun 14, 2013 at 11:01
Add a ment  | 

2 Answers 2

Reset to default 6

Use :target:

/* I'm obviously assuming you're using h2 elements for headers,
   but adjust to taste */
h2:target {
    background-color: #ffa;
}

Or, if you need to support older browsers without :target support:

var el = document.getElementById(document.location.hash);
el.style.backgroundColor = '#ffa';

And, with a function:

function hashChanged(){
    var el = document.getElementById(document.location.hash);
    el.style.backgroundColor = '#ffa';
}

document.addEventListener('hashChange', hashChanged, false);

Or, to use classes (as suggested by @Fabrizio, which I should have thought of myself):

function hashChanged(){
    var el = document.getElementById(document.location.hash),
        highlight = 'headerHighlight';
    document.querySelector('.' + highlight).classList.remove(highlight)
    el.classList.add(highlight);
}

document.addEventListener('hashChange', hashChanged, false);

And use CSS to define the particulars of the class:

.headerHighlight {
    background-color: #ffa;
}

So I want to highlight the header...

Use :target pseudoclass : http://css-tricks./on-target/

Just an example (with a smooth animation):

h2 {
   background: none;
   transition: background 3s linear 0; 
}

h2:target {
   background: #000;
}
发布评论

评论列表(0)

  1. 暂无评论