2011-04-12 83 views
0

即時嘗試爲圖像創建邊框時,我將鼠標懸停但它不添加它:S但我已嘗試console.log,它顯示文字我已寫入,但它dosent添加類到它:小號jquery addclass問題

這是我的代碼

<style> 
.test { 
    border: 1px solid #000; 
} 
</style> 
// add a class to the image that is focus on 
$('#small-image .thumb').hover(function() { 
    $(this).addClass('test'); 
     console.log('hover'); 
}, function() { 
    $(this).removeClass('test'); 
     console.log('out'); 
}); 
+0

你爲什麼要使用jQuery和CSS不? '.thumb:hover {border:1px solid#000}'? – Phil 2011-04-12 21:32:10

+0

你有一個jsfiddler.net的例子,你在做什麼? – LeRoy 2011-04-12 21:33:39

回答

0

爲什麼你需要的JavaScript的呢?

標記:

<div id="foo"></div> 

CSS:

#foo {width:50px; height:50px; border: 1px solid blue} 
#foo:hover {border-color: red} 

工作示例:http://jsfiddle.net/JndDd/

+1

':懸停'不工作在IE瀏覽器,除非該元素是一個錨點 – Neal 2011-04-12 21:33:32

+0

@Neal對於IE 7及以下;這在我看來不是問題。 – Phil 2011-04-12 21:37:09

+0

好吧,我想到那:),但它仍然沒有工作:我嘗試#small-image.thumb:懸停{0} {0} {0} {0} border:5px solid blue; } – ole 2011-04-12 21:54:20

0

問題也許是你的基地覆蓋CSS樣式在test類?

如果你不在乎IE6,你可以做整個事情與CSS:

#small-image.thumb:hover { 
    border: 5px solid blue; 
} 
0

試圖在你選擇刪除的空間:

$('#small-image .thumb') 

$('#small-image.thumb')