[英]Javascript, set div color to another divs color
我试过了:
document.getElementById('colorsdiv').style.backgroundColor =
document.getElementById(thediv).style.backgroundColor;
我知道thediv是有效的,因为如果我这样做: document.getElementById(thediv).style.backgroundColor = 'red';
它变成红色。
如果我这样做: alert(document.getElementById(thediv).style.backgroundColor);
它为null
。
我也知道在样式表中将其设置为一种颜色,因为它以这种方式显示。
我需要做什么才能使它正常工作?
要获取另一个元素的当前样式,请使用以下命令:
if( typeof getComputedStyle == "undefined")
getComputedStyle = function(elem) {return elem.currentStyle;};
document.getElementById('colorsdiv').style.backgroundColor
= getComputedStyle(document.getElementById(thediv)).backgroundColor;
您是否确定在页面完全加载后运行找到div颜色的javascript,例如它在html的body部分中。 这样可以确保页面已加载并呈现颜色,以便脚本可以在运行时检索它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.