我有一个链接到它的.css文件的页面.假设其中包含以下内容:
.wrapper {
color:red;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在后面的代码中读取.wrapper元素运行时的值?
问题是运行时链接了不同的样式表,因此.wrapper始终可用,但内容总是不同的.
我想要完成的是从当前附加的样式表中获取值(在本例中:color:red;).
那可能吗?
编辑:它可能是客户端,然后我可以把它放在一个隐藏的领域
<script type="text/javascript">
window.getComputedStyle = window.getComputedStyle || function (element) { return element.currentStyle; };
window.onload = function () {
var dummy = document.createElement("span");
dummy.className = "wrapper";
document.getElementsByTagName("body")[0].appendChild(dummy);
document.getElementById("<%= WrapperColorHiddenField.ClientID %>").value = getComputedStyle(dummy).color;
document.getElementsByTagName("body")[0].removeChild(dummy);
}
</script>
Run Code Online (Sandbox Code Playgroud)
该解决方案的缺点是您需要解析返回值:在 IE 中red返回值,而在 Chrome 中返回值rgb(255,0,0)
| 归档时间: |
|
| 查看次数: |
1851 次 |
| 最近记录: |