2013-07-16 22 views
7

jquery: width: auto kullanarak bir öğenin satır içi özelliğini almaya çalışıyorum. Genişliği alır almaz, .width(), auto yerine bir px değeri döndürür. Bu neye ihtiyacım örneğidir: Ben setted bu satır içi stili ile bir img ettikJQuery - Genişlik al: satır içi stilden otomatik

:

<img id='image' style='position: absolute; height: 200px; width: auto; top: 25px; left: 50px;' src='http://tinyurl.com/k8ef66b'/> 

Ben görüntü tıklanabilir hale getirmek, bir a içinde bu görüntüyü kaydırmak gerekir. Ben görüntü stilini almak ve bağlantı etiketi taşımak için de gereklidir: http://jsfiddle.net/cBXAz/1/

bir fikrin:

burada
//--- get height, width and entire style 
var imgHeight = $("#image").height(); 
var imgWidth = $("#image").width(); 
var imgStyle = $("#image").attr("style"); 

//---remove inline style from the img 
$("#image").removeAttr("style"); 

//--- create the <a>, add the inline style 
var anch = $("<a href='#'></a>"); 
$(anch).attr("style", imgStyle); 

//--- add back to the img it's width and height 
//--- the problem is here: imgWidth does not contain 'auto' 
$("#image").css({"width" : imgWidth, "height" : imgHeight}); 

//--- wrap the img 
$("#image").wrap(anch); 

koduyla bir keman var? auto'u bu satır içi stilden nasıl alabilirim? Görüntüye bir px değeri yerine width: auto vermem gerekiyor. peşin

sayesinde saygılarımla

+1

'$ ("# resim") css ({ "width": "auto"}) '??? – Morpheus

+0

Kulağa baktığımda görüntüde 'height: auto' görmüyorum. –

+0

@KevinBowersox "width: auto" yazdım, "height" değil :) – BeNdErR

cevap

7
document.getElementById('image').style.width; 

Ya da daha jQuery'nin yol.

$("#image")[0].style.width; 
+0

Bu durumda gerçek genişlik elde edeceksiniz görüntü, otomatik değil :) – Morpheus

+0

http://jsfiddle.net/JA2ds/ Eğer genişlik değiştirmeden önce kullanırsanız, emin çalışır –

+0

Diğer jQuery yolu, $ ("# image"). width() 'dir. veya $ ("# image"). css ('genişlik') ' – jQuery00