简体   繁体   English

如何获得图像真正的css显示属性

[英]how to get image true css display property

i got a piece of html 我有一块HTML

<img style="cursor: pointer; width: auto; height: auto; display: inline;" src="http://www.kidsgen.com/fables_and_fairytales/images/rapunzel.gif" alt="rapunzel" title="rapunzel" align="right">

and even if i set display: inline; 即使我设置display: inline; in its style, when i'm trying to get its css display property like this: 在它的风格,当我想要得到这样的css显示属性:

alert($('img:first').css('display'))

or 要么

var el=document.getElementsByTagName('img')[0]
alert(document.defaultView.getComputedStyle(el,null)['display'])

it always gives me value block . 它总是给我价值block

what's wrong? 怎么了?

The align='right' property assignment is causing the img element to have its display property set to 'block'. align='right'属性赋值使img元素的display属性设置为'block'。 Your code without the align='right' propery will alert 'inline' on jsFiddle. 没有align='right' 属性的代码会在jsFiddle上提醒'内联'

<body>
    <img style="cursor: pointer; width: auto; height: auto; display: inline;" src="http://www.kidsgen.com/fables_and_fairytales/images/rapunzel.gif" alt="rapunzel" title="rapunzel" />
</body>

alert($('img:first').css('display')); // alerts 'inline'

A relevant piece of extra information is img tags are in fact inline elements by default. 相关的额外信息是img标签,实际上是默认的内联元素。 However, with align='right' set inside the img tag, I was unable to set the display property back to inline even by inserting this line of code: 但是,在img标记内设置了align='right' ,即使插入以下代码行,我也无法将显示属性设置回内联:

$('img:first').css('display', 'inline');

Its because you have align="right" in the image tag; 这是因为你在图片标签中有align="right" ;

That CSS rule is used to align block elements ( Learn more ). 该CSS规则用于对齐块元素( 了解更多信息 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM