BeN*_*ErR 7 javascript css jquery
我正在尝试使用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'/>
Run Code Online (Sandbox Code Playgroud)
我需要将图像包装成一个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);
Run Code Online (Sandbox Code Playgroud)
这里是代码的小提琴:http://jsfiddle.net/cBXAz/1/
任何的想法?如何auto摆脱内联风格?我需要给width: auto图像而不是px值.
在此先感谢,最好的问候
document.getElementById('image').style.width;
Run Code Online (Sandbox Code Playgroud)
或者更多jquery的方式:
$("#image")[0].style.width;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7928 次 |
| 最近记录: |