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
을 이미지에 제공해야합니다. 사전에
감사합니다, 감사합니다
'$ ("# 이미지") CSS ({ "폭": "자동"}) '??? – Morpheus
피들을 볼 때 이미지에 '높이 : 자동'이 표시되지 않습니다. –
@KevinBowersox "높이"가 아닌 "너비 : 자동"이라고 적었습니다. – BeNdErR