<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档 </title>
<style type="text/css">
div#aa div{WIDTH:300px; HEIGHT:200px; background:#ff0000;margin:10px}
div#aa .classa{WIDTH:200px; HEIGHT:80px;background:#f6b3ad}
div#aa .classb{WIDTH:100px; HEIGHT:60px;background:#f6b3ad}
</style>
</head>
<body>
<div id="aa" style="WIDTH:800px; HEIGHT:500px;background:#808000">
<div id="aa1">
<div class="classa"></div>
<div class="classb"></div>
</div> <div id="aa2">
<div class="classa"></div>
<div class="classb"></div>
</div>
<script language="javascript">
var mydiv = document.getElementById('aa2');
var xxx=mydiv.getElementsByTagName("div");
for(var j=0; j<kkk.length; j++)
{
if(xxx[j].currentStyle) {alert(xxx[j].currentStyle['WIDTH'])}
else if(window.getComputedStyle) {alert(window.getComputedStyle(xxx[j], null)['WIDTH'])}
}
</script>
</body>
</html>
//虽有在CSS中定义div标签的WIDTH样式,为什么用js取不出来相应的值??
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档 </title>
<style type="text/css">
div#aa div{WIDTH:300px; HEIGHT:200px; background:#ff0000;margin:10px}
div#aa .classa{WIDTH:200px; HEIGHT:80px;background:#f6b3ad}
div#aa .classb{WIDTH:100px; HEIGHT:60px;background:#f6b3ad}
</style>
</head>
<body>
<div id="aa" style="WIDTH:800px; HEIGHT:500px;background:#808000">
<div id="aa1">
<div class="classa"></div>
<div class="classb"></div>
</div> <div id="aa2">
<div class="classa"></div>
<div class="classb"></div>
</div>
<script language="javascript">
var mydiv = document.getElementById('aa2');
var xxx=mydiv.getElementsByTagName("div");
for(var j=0; j<kkk.length; j++)
{
if(xxx[j].currentStyle) {alert(xxx[j].currentStyle['WIDTH'])}
else if(window.getComputedStyle) {alert(window.getComputedStyle(xxx[j], null)['WIDTH'])}
}
</script>
</body>
</html>
//虽有在CSS中定义div标签的WIDTH样式,为什么用js取不出来相应的值??
<div id="aa1">
<div class="classa"></div>
<div class="classb"></div>
</div>
</div>这里少个关闭这一段的div根本没有关闭.楼主检查一下for(var j=0; j<kkk.length; j++) kkk是什么????
xxx[j].currentStyle.width
div#aa div{WIDTH:300px; HEIGHT:200px; background:#ff0000;margin:10px}
div#aa .classa{WIDTH:200px; HEIGHT:80px;background:#f6b3ad}
div#aa .classb{WIDTH:100px; HEIGHT:60px;background:#f6b3ad}
</style>
</head>
<body>
<div id="aa" style="WIDTH:800px; HEIGHT:500px;background:#808000">
<div id="aa1">
<div class="classa"></div>
<div class="classb"></div>
</div>
<div id="aa2">
<div class="classa"></div>
<div class="classb"></div>
</div>
</div><!-- 这里要封闭 -->
<script language="javascript">
var mydiv = document.getElementById('aa2');
var xxx=mydiv.getElementsByTagName("div");
for(var j=0; j<xxx.length; j++) //这里应该是xxx不是kkk
{
if(xxx[j].currentStyle) {alert(xxx[j].currentStyle.width)} //这里用currentStyle.width
else if(window.getComputedStyle) {alert(window.getComputedStyle(xxx[j], null).width)}
}
</script>
也就是:
上部分代码相同,下面代码中的width换成background就不行了?
{
if(xxx[j].currentStyle) {alert(xxx[j].currentStyle.background)} else if(window.getComputedStyle) {alert(window.getComputedStyle(xxx[j], null).background)}
}
if(xxx[j].currentStyle) {alert(xxx[j].currentStyle.backgroundColor)} //这里用currentStyle.backgroundColor
else if(window.getComputedStyle) {alert(window.getComputedStyle(xxx[j], null).backgroundColor)}
image Any of the range of image values available to the backgroundImage property.
repeat Any of the range of repeat values available to the backgroundRepeat property.
attachment Any of the range of attachment values available to the backgroundAttachment property.
position Any of the range of position values available to the backgroundPosition property.
写在一起的默认值是:background: transparent none repeat scroll 0% 0%
要取背景色就写具体的 backgroundColor 了
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script language="javascript">
function toHexColor(rgbColor) {
var re = /^rgb/i,
reExtract = /^rgb\((\d+),(\d+),(\d+)\)/gi;
var hexRGB = "#", temp;
if(re.test(rgbColor)) {
reExtract.exec(rgbColor);
temp = parseInt(RegExp.$1);
hexRGB += temp < 16? "0" + temp.toString(16): temp.toString(16);
temp = parseInt(RegExp.$2);
hexRGB += temp < 16? "0" + temp.toString(16): temp.toString(16);
temp = parseInt(RegExp.$3);
hexRGB += temp < 16? "0" + temp.toString(16): temp.toString(16);
rgbColor = hexRGB;
}
return rgbColor;
}</script>
<title>无标题文档 </title>
<style type="text/css">
div#aa div{WIDTH:300px; HEIGHT:200px; background:#ff0000;margin:10px}
div#aa .classa{WIDTH:200px; HEIGHT:80px;background:#f6b3ad}
div#aa .classb{WIDTH:100px; HEIGHT:60px;background:#f6b3ad}
</style>
</head>
<body>
<div id="aa" style="WIDTH:800px; HEIGHT:500px;background:#808000">
<div id="aa1">
<div class="classa"></div>
<div class="classb"></div>
</div> <div id="aa2">
<div class="classa"></div>
<div class="classb"></div>
</div></div>
<script language="javascript">
var mydiv = document.getElementById('aa2');
var xxx=mydiv.getElementsByTagName("div");
for(var j=0; j<xxx.length; j++)
{
if(xxx[j].currentStyle) {alert(xxx[j].currentStyle.backgroundColor)}
else if(window.getComputedStyle) {var yyy=window.getComputedStyle(xxx[j], null).backgroundColor;alert(toHexColor(yyy));}
}</script>
</body>
</html>
<div id="aa1">
<div class="classa"></div>
<div class="classb"></div>
</div>
</div>这里少个关闭这一段的div根本没有关闭.楼主检查一下
看看行了不?<script language="javascript">
function toHexColor(rgbColor) {
//alert(rgbColor)
var re = /^rgb/i,
reExtract = /^rgb\((\d+),\s(\d+),\s(\d+)\)/gi; //正则表达式中\s代表空格
var hexRGB = "#", temp;
if(re.test(rgbColor)) {
reExtract.exec(rgbColor);
temp = parseInt(RegExp.$1);
hexRGB += temp < 16? "0" + temp.toString(16): temp.toString(16);
temp = parseInt(RegExp.$2);
hexRGB += temp < 16? "0" + temp.toString(16): temp.toString(16);
temp = parseInt(RegExp.$3);
hexRGB += temp < 16? "0" + temp.toString(16): temp.toString(16);
rgbColor = hexRGB;
}
return rgbColor;
}
</script>
reExtract = /^rgb\(\s?(\d+),\s?(\d+),\s?(\d+)\)/i; //去掉了全局标记g