jquery: を使用して要素のインライン プロパティを取得しようとしていますwidth: auto
。幅を取得するとすぐに、.width()
代わりに px 値を返しますauto
。これは私が必要とするものの例です:
私はimg
このインラインスタイルを設定しました:
<img id='image' style='position: absolute; height: 200px; width: auto; top: 25px; left: 50px;' src='http://tinyurl.com/k8ef66b'/>
a
画像をクリックできるようにするには、その画像を でラップする必要があります。また、画像スタイルを取得してアンカー タグに移動する必要があります。
//--- 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);
ここにコードのフィドルがあります:http://jsfiddle.net/cBXAz/1/
何か案が?auto
そのインライン スタイルから抜け出すにはどうすればよいですか? width: auto
px 値の代わりに画像を指定する必要があります。
よろしくお願いします。