2013-01-09 37 views
1

的CSS圖片標籤style.height不覆蓋css類的高度?

.myStyle { 
height: 10px; 
background-image: url(../myImage.png); 
} 

HTML

<img class=myStyle src=<%scriptlet%> > 

現在的問題是在我的JS,我已經寫了這個img標籤一個錯誤處理程序,這將設置高度爲0px。這0px來作爲style.height = 0px但不覆蓋類myStyle的高度。

JS

myImage = $('.myStyle', $el); 
myImage.error(function() { 
      myImage.attr('style.height', '0px'); 
}); 
+2

你應該閱讀這個http://jqfundamentals.com/chapter/jquery-basics –

+0

我已經發布了我的第二個問題http://stackoverflow.com/questions/14230762/read-the-background-image-attribute-值得一提的是mystyle-in-error-handler- – Nik

+0

感謝大家的及時迴應。 – Nik

回答

4

變化:

myImage.attr('style.height', '0px'); 

myImage.css('height', '0'); 

要獲得背景圖像,你可以用得到,像

myImage.css("background"); 

OR

myImage.css("backgroundImage") 
+0

謝謝。這工作。你是否也知道我的第二個問題的解決方案? myImage.attr('src',background-image);? – Nik

1

使用此:

myImage.height(0); 

相反的:

myImage.attr('style.height', '0px'); 

attr()是爲了屬性操作做出,所以後來申報工作,您也可以將其更改爲:

myImage.attr('style', 'height: 0px;'); 

參見attr() on the jQuery docs

0

使用這個代替:

myImage.height('10px'); 
0
myImage.style.height = '0px'; 

這也將正常工作。