8

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: autopx 値の代わりに画像を指定する必要があります。

よろしくお願いします。

4

1 に答える 1

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

または、より多くのjqueryの方法について:

$("#image")[0].style.width;
于 2013-07-16T09:07:48.113 に答える