JQuery - 从内联样式获取宽度:自动 [英] JQuery - Get width: auto from inline style

查看:156
本文介绍了JQuery - 从内联样式获取宽度:自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jquery获取元素的内联属性: width:auto
一旦我得到宽度, .width()就会返回一个px值而不是 auto
这是我需要的一个例子:

I'm trying to get an inline property of an element using jquery: width: auto. As soon as I get the width, .width() returns a px value instead of auto. This is an example of what I need:

我有一个 img 这个内联样式设置:

I've an img with this inline style setted:

<img id='image' style='position: absolute; height: 200px; width: auto; top: 25px; left: 50px;' src='http://tinyurl.com/k8ef66b'/>

我需要将该图像包装在 a ,使图像可点击。我还需要获取图像样式并将其移动到锚标记:

I need to wrap that image in an a, to make the image clickable. I need also to get the image style and move it to the anchor tag:

//--- 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/

here's a fiddle with the code: http://jsfiddle.net/cBXAz/1/

任何想法?如何从内联样式中获取 auto ?我需要将 width:auto 提供给图片而不是px值。

any idea? How can I get the auto out of that inline style? I need to give width: auto to the image instead of a px value.

提前致谢,最好的问候

推荐答案

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

或更多jquery方式:

Or for more jquery's way:

$("#image")[0].style.width;

这篇关于JQuery - 从内联样式获取宽度:自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆