如何获得计算样式? [英] How do I get a computed style?
问题描述
任何人都请帮助我一个脚本..或一种方式获得
height:1196px;
width:284px;
。我知道IE是不同的 - 像往常一样。我无法访问iframe(跨域) - 我只需要高度/宽度。
我需要的屏幕截图(以红色圈出)。如何访问这些属性?
源
< iframe id =frameId src =anotherdomain\brsstart.htm>
< html id =brshtmlxmlns =http://www.w3.org/1999/xhtml>
\ - 我想要此元件COMPUTED BROWSER CSS HEIGHT / WIDTH
< head>
< title>未命名的专页< / title>
< / head>
< body>
BLA BLA BLA STUFF
< / body>
< / html>
\ --- $('#frameId')。context.lastChild.currentStyle
*这获得在其他域上的实际原始样式集auto
*现在如何获取计算风格?
< / iframe>
我得到的最接近的是
$('#frameId')。context.lastChild.currentStyle
这给我的HTML元素的实际样式是自动,这是真实的,因为它在iframed文档上的设置。
如何获得
使用Tomalaks答案我想起了这个可爱的webkit脚本
window.getComputedStyle(document.getElementById(frameId),null).getPropertyValue(height)
或
window.getComputedStyle document.getElementById(frameId),null).getPropertyCSSValue(height)。cssText
结果150px
与
相同
$('#frameId')。高度();
所以我让他们在头部添加一个'brshtml'选择元素更容易。 Webkit检查显示我现在是html#brshtml但我不能选择它使用 getelementbyid
查看此回答。
这不是jQuery,但在Firefox,Opera
和Safari使用
window.getComputedStyle(element)
到
获取元素
的计算样式,在IE中可以使用
element.currentStyle
。返回的
对象在每种情况下是不同的,
和我不知道如何工作
元素和样式创建使用
Javascript,但也许他们将
有用。
iframe
看起来高150px。如果其内容的高度为1196px(确实,您似乎正在浏览 html
节点,根据截图),这就是你想要的get,那么您应该导航到iframe文档的DOM ,并应用上述技术。 p>
Can anybody please help me with a script.. or a way to get the value of
height : 1196px;
width: 284px;
from the computed style sheet (webkit). I know IE is different- as usual. I cannot access the iframe (cross domain)—I just need the height/width.
Screenshot of what I need (circled in red). How do I access those properties?
Source
<iframe id="frameId" src="anotherdomain\brsstart.htm">
<html id="brshtml" xmlns="http://www.w3.org/1999/xhtml">
\--I WANT THIS ELEMENTS COMPUTED BROWSER CSS HEIGHT/WIDTH
<head>
<title>Untitled Page</title>
</head>
<body>
BLA BLA BLA STUFF
</body>
</html>
\--- $('#frameId').context.lastChild.currentStyle
*This gets the actual original style set on the other domain which is "auto"
*Now how to getComputed Style?
</iframe>
The closest I got is this
$('#frameId').context.lastChild.currentStyle
That gives me the actual style on the HTML element which is "auto" and that is true as thats what's its set on the iframed document.
How do I get the computed style that all the browsers use to calculate the scroll bars, and inspect elements values?
Using Tomalaks answer I conjured up this lovely piece of script for webkit
window.getComputedStyle(document.getElementById("frameId"), null).getPropertyValue("height")
or
window.getComputedStyle(document.getElementById("frameId"), null).getPropertyCSSValue("height").cssText
Result 150px
Identical to
$('#frameId').height();
So I got them to add a id of 'brshtml' to the head- maybe it will help me select the element easier. Webkit inspection shows me now html#brshtml but I cant select it using getelementbyid
See this answer.
It's not jQuery but, in Firefox, Opera and Safari you can use
window.getComputedStyle(element)
to get the computed styles for an element and in IE you can useelement.currentStyle
. The returned objects are different in each case, and I'm not sure how well either work with elements and styles created using Javascript, but perhaps they'll be useful.
The iframe
looks about 150px high to me. If its contents are 1196px high (and indeed, you appear to be exploring the html
node, according to the screenshot) and that's what you want to get, then you should navigate into the DOM of the iframe's document and apply the above technique to that.
这篇关于如何获得计算样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!