我如何获得计算样式?

Pio*_*ula 38 html javascript css cross-domain computed-style

任何人都可以帮我一个脚本..或一种获得价值的方法

height : 1196px;
width: 284px;
Run Code Online (Sandbox Code Playgroud)

从计算样式表(webkit).我知道IE与往常不同.我无法访问iframe(跨域) - 我只需要高度/宽度.

我需要的屏幕截图(用红色圈出).我如何访问这些属性?

在此输入图像描述

资源

<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>
Run Code Online (Sandbox Code Playgroud)

我得到的最接近的是这个

$('#frameId').context.lastChild.currentStyle
Run Code Online (Sandbox Code Playgroud)

这给了我HTML元素的实际样式,即"auto",这就是iframed文档中的设置.

如何获得所有浏览器用于计算滚动条的计算样式,并检查元素值?

使用Tomalaks回答我为webkit制作了这个可爱的脚本

window.getComputedStyle(document.getElementById("frameId"), null).getPropertyValue("height")
Run Code Online (Sandbox Code Playgroud)

要么

window.getComputedStyle(document.getElementById("frameId"), null).getPropertyCSSValue("height").cssText
Run Code Online (Sandbox Code Playgroud)

结果150px

相同

$('#frameId').height();
Run Code Online (Sandbox Code Playgroud)

所以我让他们在头部添加一个'brshtml'的id-也许它会帮助我更容易地选择元素.Webkit检查现在向我显示html #brshtml但我无法使用它来选择它getelementbyid

Lig*_*ica 42

看到这个答案.

这不是jQuery,但是,在Firefox,Opera和Safari中,您可以使用它 window.getComputedStyle(element)来获取元素的计算样式,并在IE中使用 element.currentStyle.返回的对象在每种情况下都是不同的,我不确定使用Javascript创建的元素和样式有多好,但也许它们会很有用.

iframe大约150像素高,以我的外表.如果它的内容是1196px高(事实上,你似乎正在探索html节点,根据屏幕截图),这就是你想要获得的,那么你应该导航到iframe文档的DOM并应用上述技术.

  • @geekchic:是的,这就是我的回答. (4认同)
  • @Tomalak哈,所以确实如此.略读失败. (2认同)

小智 11

看看https://developer.mozilla.org/en-US/docs/Determining_the_dimensions_of_elements

使用.clientWidth获取px中的整数宽度.

<div id="mydiv" style="border:1px solid red;">This is DIV contents.</div>
<button onclick="alert(
document.getElementById('mydiv').clientWidth);">
   Click me to see DIV width in px
</button>
Run Code Online (Sandbox Code Playgroud)