Javascript style.color不起作用 [英] Javascript style.color doesn't work
本文介绍了Javascript style.color不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function myFunction() {
alert(document.getElementById("myP").style.color);
}
#myP {
color: pink;
}
<!DOCTYPE html>
<html>
<body>
<p id="myP">This is an example paragraph.</p>
<button type="button" onclick="myFunction()">Return text color of p</button>
</body>
</html>
如果我这样做
<p id="myP" style="color: pink;">This is an example paragraph.</p>
它正确返回颜色值.
如果在CSS中设置了颜色值,如何获得颜色值?
How can I get the color value if the value is set in css ?
我需要Javascript,而不是JQuery.
I need Javascript not JQuery.
谢谢.
推荐答案
尝试此代码
var element = document.getElementById('myP'),
style = window.getComputedStyle(element),
color = style.getPropertyValue('color');
alert(color);
这篇关于Javascript style.color不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文