使用JavaScript获取CSS值 [英] Get a CSS value with JavaScript
本文介绍了使用JavaScript获取CSS值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以通过JavaScript来设置CSS值,例如:
I know I can set a CSS value through JavaScript such as:
document.getElementById('image_1').style.top = '100px';
但是,我可以获取当前特定的样式值吗?我已经阅读了哪里可以得到整个样式的元素,但我不想解析整个字符串,如果我不必。
But, can I get a current specific style value? I've read where I can get the entire style for the element, but I don't want to have to parse the whole string if I don't have to.
推荐答案
您可以使用 getComputedStyle()
。
You can use getComputedStyle()
.
var element = document.getElementById('image_1'),
style = window.getComputedStyle(element),
top = style.getPropertyValue('top');
jsFiddle 。
这篇关于使用JavaScript获取CSS值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文