2013-07-16 1 views
7

jquery : width: auto을 사용하여 요소의 인라인 속성을 가져 오려고합니다. 폭을 얻 자마자 .width()auto 대신 px 값을 반환합니다. 이 내가 필요의 예입니다 : 내가 살고 있고이 인라인 스타일을 가진 img을했습니다JQuery - 인라인 스타일에서 너비 : 자동 가져 오기

:

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

내가 이미지를 클릭 할 수 있도록하기 위해, a에서 해당 이미지를 포장해야합니다. 나는 이미지 스타일을 얻고 그것이 앵커 태그로 이동하는 것이 필요 : http://jsfiddle.net/cBXAz/1/

어떤 생각 :

여기
//--- 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); 

코드와 바이올린입니까? 그 인라인 스타일에서 auto을 어떻게 얻을 수 있습니까? px 값 대신에 width: auto을 이미지에 제공해야합니다. 사전에

감사합니다, 감사합니다

+1

'$ ("# 이미지") CSS ({ "폭": "자동"}) '??? – Morpheus

+0

피들을 볼 때 이미지에 '높이 : 자동'이 표시되지 않습니다. –

+0

@KevinBowersox "높이"가 아닌 "너비 : 자동"이라고 적었습니다. – BeNdErR

답변

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

이상의 jQuery의 방법을 :.

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

이 경우 너는 실제 너비 이미지가 아닌'auto' :) – Morpheus

+0

http://jsfiddle.net/JA2ds/ 폭을 변경하기 전에 사용한다면 확실하게 작동합니다. –

+0

더 많은 jQuery 방법은'$ ("# image")입니다. 또는 '$ ("# image"). css ('width ')' – jQuery00

관련 문제