图像尺寸奇怪
el = function(q) {return document.getElementById(q)};
el('strange').style.height = '100px'
el('strange').height = 2000
alert(el('strange').height) // 100?
alert(el('strange').getAttribute('height')) //2000? Wait.. What?
el 是 document.getElementById 的简写。有人可以解释一下发生了什么事吗?我怀疑 height 属性与 height 属性略有不同:他们修改了它,以便它返回计算的样式。我不确定,因为 DOM 0 规定该属性应该与 getAttribute 相同,但锚点的 href 属性与大多数浏览器中的 getAttribute 不匹配。并且:
https://developer.mozilla.org/en/DOM/HTMLImageElement
HTML :
<img id="strange" src="http://images.devshed.com/fcw/belts/fcw_forums.gif" />
el = function(q) {return document.getElementById(q)};
el('strange').style.height = '100px'
el('strange').height = 2000
alert(el('strange').height) // 100?
alert(el('strange').getAttribute('height')) //2000? Wait.. What?
el is a shorthand of document.getElementById. Can someone explain me what's going on? I suspect that the height property is slightly different than the height attribute: they modified it so it returns the computed style. I'm not sure, because DOM 0 says that the property should be the same as the getAttribute, but the href property of an anchor doesn't match with the getAttribute in most browsers. And:
https://developer.mozilla.org/en/DOM/HTMLImageElement
The HTML:
<img id="strange" src="http://images.devshed.com/fcw/belts/fcw_forums.gif" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你发现了的话,这个奇怪的现象并不完全是这样。这是因为作为 setter 调用 height 属性是在 img 标签中创建一个 html height 属性,据我所知,这是一个仅用于 canvas 的属性。这个html标签和dom值之间没有任何联系。
如果我执行以下操作:
输出将为
100
和100px
DOM 上的高度是正确的。但是,使用 getAttribute 搜索 html 标记中的属性,因此返回“2000”。编辑
好吧,我想我明白了,
有 3 种不同的东西:
css 高度、高度属性、高度 DOM 值。
最简单的是 DOM 值。它总是返回以 css 像素为单位的 img 实际高度。如果通过css设置,则根据css值计算,如果通过属性设置,则根据css值计算。
现在另外两个。
它们都指定 img 尺寸。但 css 值优先于 HTML 属性。
w3 建议中对此进行了说明。我引用
的是
声明如下
因此,img(我认为是内联块元素)使用高度CSS值,但如果将其设置为auto(并且这是默认值),则它使用
固有高度
。这就是 html 属性。因此,调用
strange.height
作为 getter 获取 DOM 值,作为 setter,它设置 HTML 属性。EDIT2 为了更准确地回答,你有 3 个基本规则:
(这里是真实高度)你只是不能遵循 3 个规则如果 CSS 和属性值均已指定且不同。
重点是 DOM 应该 是相同的。这里如果有CSS就不可能这样,所以它有不同的值。
作为旁注,这里对 height 属性的使用有一个很好的解释:http://reference.sitepoint。 com/html/img 。
The oddity isn't exaclty were you're spotting it. It comes from the fact that the call to the height attribute as a setter is creating a html height attribute in the img tag, and as far as I know, it's an attribute used only for canvas. There is no connection between this html tag and the dom value.
If i do the following:
the output will be
100
and100px
the height on the DOM is correct. However, using the getAttribute search for the attributes in the html tag, therefor returning "2000".EDIT
Ok i think i got it
There are 3 different stuff:
The css height, the height attribute, the height DOM value.
The easiest is the DOM value. It always return the img real height in css pixel. If set trhough css, it will be based on the css value, if set through attribute, it will be calculated from that.
Now the two other.
They both specify the img dimension. But the css value as precedence over the HTML attribute.
This is stated in the w3 recommandation. I quote
For
It's stated that
Therefore, img (who are inline-block element I think) use the height css value, but if this one is set to auto (and it's the default) it uses the
intrinsic height
. And that is the html attribute.So calling
strange.height
as a getter gets the DOM value, and as a setter, it sets the HTML attribute.EDIT2 And to answer more exactly, you have 3 basic rules:
You just can't follow the 3 rules if both CSS and attribute value are specified and differ.
The point is DOM should be the same. Here it can't be if there is CSS, so it has a different value.
As a side note, a nice explanation of the use of the height attribute here: http://reference.sitepoint.com/html/img .