我有一個頭元素裏面有一個錨元素存在。以下是代碼:OnClick的錨標籤的內容不應該顯示
<h3 class="header"><a href="http://www.google.com">Google</a></h3>
單擊標題元素,一些文本顯示在下面。爲此,我使用了jQuery。以下是代碼:
$('.header').click(function() {
$header = $(this);
$anchor = $header.find("a");
$content = $header.next(); // Get next element
$content.slideToggle(100, function() {
$content.is(":visible") ? $anchor.css({'color': 'white'}) : $anchor.css({'color': '#1818bd'});
$content.is(":visible") ? $header.css({'background-color': 'black'}) : $header.css({'background-color': '#cccccc'});
});
});
在錨點元素懸停時,文本變爲紅色。以下是代碼:
a:hover {color: red;}
問題1:單擊錨點元素時,我不希望顯示下面的文本內容。這應該只發生在按預期發生的標題元素上。
問題2:點擊標題元素後,出現下面的文本內容。現在,如果將鼠標懸停在錨點元素上,文字顏色不會變爲紅色。
下面是JS提琴鏈接: https://jsfiddle.net/heisenberg_kl3/np5pmqLq/
問題2解決。請提供一個解決問題1.
加上':懸停{顏色:紅色重要;}'在你的CSS –
你的第一個問題解決在這個問題:http://stackoverflow.com/questions/1369035/how-do-i-prevent -a-parents-on-click-event-from-firing-when-a-child-anchor-is-cli –