Próbuję uzyskać właściwość wstawiania elementu za pomocą jquery: width: auto
. Gdy tylko otrzymam szerokość, .width()
zwraca wartość px zamiast auto
. To jest przykład tego, co trzeba:JQuery - Get width: auto from inline style
mam takie img
z tego stylu inline setted:
<img id='image' style='position: absolute; height: 200px; width: auto; top: 25px; left: 50px;' src='http://tinyurl.com/k8ef66b'/>
muszę zawinąć ten obraz w a
, aby klikalny obrazu. Muszę również uzyskać styl obrazu i przenieść go do znacznika zakotwiczenia:
//--- 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);
oto skrzypce z kodem: http://jsfiddle.net/cBXAz/1/
jakiś pomysł? Jak mogę usunąć auto
z tego wbudowanego stylu? Muszę nadać obrazowi width: auto
zamiast wartości px.
góry dzięki, Z poważaniem
'$ ("# obraz") css ({ "szerokość": "auto"}) '??? – Morpheus
Kiedy patrzę na skrzypce, nie widzę "height: auto" na obrazie. –
@KevinBowersox Napisałem "width: auto", a nie "height" :) – BeNdErR