2014-01-21 44 views
3

I'm working on a project where I need to hide a div when the a x is clicked. I'm wanting to target a specific div to be closed, the div that is holding the href nice. I'm getting a bit stuck with targeting the div once the X is clicked. Could you give any pointers for simple designer?隱藏使用<a href="#nice">

HTML

Div name = footer and the link class is "linky"

CSS

 .linky:link { 
     footer:display: none; 
     } 

Any help would be appreciated.

Regards,

回答

4

Put the link target directly before the footer:

<body> 
    <a href="#linky">hide footer</a> 
    ... other content ... 
    <div id="linky"></div> 
    <footer>footer</footer> 
</body> 

the use the + operator:

#linky:target + footer { 
    display: none; 
} 

/* to prevent scrolling to the bottom */ 
#linky { 
    position: fixed;  
    top: 0; 
} 

see this fiddle

a + b將目標b如果是直接a後,我的股利。

注意從您的版本的另外兩個區別:

  • 使用:target而不是:link:link將目標鏈接)
  • 使用id而不是class

我想補充只要用戶點擊任何其他#anchor鏈接,頁腳就會再次顯示。

更新:現在阻止滾動;除去簡單的版本,因爲滾動無法避免有

+0

您好,感謝您的嘖嘖。我花了一點時間注意到你的小提琴文字消失在底部。我必須承認我現在已經完成了這個項目,但是謝謝你的幫助,稍後會回來。 – user3082874

0

試試這個:

<div id="div1"> 
    Hello, i will be hidden on click the below link 
</div> 

<a href="" id="link" onClick="hide()"><b>Click here</b></a> to hide 
## this is the link code 
<a href="" id="link" onClick="hide()"><b>Click here</b> to hide 
<script> 
    function hide(){ 
     document.getElementById('div1').style.display="none"; 
    } 
</script> 
+0

你好,感謝幫助。那是JavaScript?我試過了,但沒有發生任何事......我不明白爲什麼它不應該起作用。 – user3082874