<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Test</title>
<style type="text/css">
.divStyle{
background:#0099CC;
font-size:14px;
color:#CC9933;
}
</style>
</head><body>
<div id="testDiv" class="divStyle" style="border:2px solid #000000">hello world</div>
</body>
</html>
假如上面为网页内容,我想获取 div(id为testDiv)的style内容:
获取结果应该是:
background:#0099CC;
font-size:14px;
color:#CC9933;
border:2px solid #000000;
而其他没有设定的内容则不获取,请问大虾们有什么好方法吗?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Test</title>
<style type="text/css">
.divStyle{
background:#0099CC;
font-size:14px;
color:#CC9933;
}
</style>
</head><body>
<div id="testDiv" class="divStyle" style="border:2px solid #000000">hello world</div>
</body>
</html>
假如上面为网页内容,我想获取 div(id为testDiv)的style内容:
获取结果应该是:
background:#0099CC;
font-size:14px;
color:#CC9933;
border:2px solid #000000;
而其他没有设定的内容则不获取,请问大虾们有什么好方法吗?
document.getElementById("testDiv").style.backgrounddocument.getElementById("testDiv").style.fontSizedocument.getElementById("testDiv").style.colordocument.getElementById("testDiv").style.border
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Test </title>
<style type="text/css">
.divStyle{
background:#0099CC;
font-size:14px;
color:#CC9933;
}
</style>
</head>
<script type="text/javascript">function getStyle(obj)
{
dobj = document.getElementById("testDiv2").currentStyle;
objs = obj.currentStyle
var Styles =""
for(var st in objs)
{
if(dobj[st] != objs[st])
{
Styles +=st+":";
Styles +=objs[st]+"<br/>";
}
}
document.getElementById("DDD").innerHTML = Styles ;}
</script>
<body>
<div id="testDiv" class="divStyle" onclick="getStyle(this)" style="border:2px solid #000000"> hello world </div>
<div id="testDiv2"></div>
<TABLE width="100%">
<TR>
<TD ID="DDD">
</TD>
</TR>
</TABLE>
</body>
</html>你要的那中结果是出不来的 你用CSS语言写过的样式会自动编译成他自己原定义的格式,除非你自己再写个转换工具.
<HTML >
<HEAD >
<TITLE > CSS </TITLE >
<style>
.a{
background-color:#FE9804;
}
</style>
</HEAD >
<BODY onLoad="b()">
<input type=button name= "up " value= "up " id="up" class= "a" style="width:200px" >
</BODY >
</HTML >
<script language= "JavaScript" >
function b(){
var o=document.getElementById("up");
o.value= "dfdf ";
var ss=document.styleSheets[0];
var rules = ss.cssRules?ss.cssRules:ss.rules;
for(var i=0;i <rules.length;i++){
var rule=rules[i];
for(p in rule.style){
if(rule.style[p]){
alert(p+ ": "+rule.style[p]+ "\n ");
}
}
}
}
</script>
看对你所需有帮助没,这只针对css中的所设置过的属性
var ns=navigator.appName=="Netscape";
var e=document.getElementById("controlId");
var st;
if(ns)//ff以及支持2级dom的浏览器
{
st=window.getComputedSystle(e,null);
}
else//ie
{
st=e.currentStyle;
}
其实就是一个dom元素, 我也不知道它哪些style 被设定过,但是 如果某个style被设定过,我就要把它取出来(当然也包括它的值),
例如dom元素的属性,如果被指定过 那么它的specified 则为真,不过style好像没有这个东西