<!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>
<title></title>
</head>
<body>
<div id="cc" style="color: Black; font-size: 34px;">klkjlkjlfs</div>
</body><script language="javascript" type="text/javascript">
var i=document.getElementById("cc").getAttributeNode("style").nodeValue;
alert(i);
</script></html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
</head>
<body>
<div id="cc" style="color: Black; font-size: 34px;">klkjlkjlfs</div>
</body><script language="javascript" type="text/javascript">
var i=document.getElementById("cc").getAttribute("style").cssText;
alert(i);
</script>
</html>有分不!? ....呵呵 :)
Style是属性元素吧,为什么用nodeValue读到属性元素的值呢????请您给回答一下
</body>
</html>
var i=document.getElementById("cc").getAttribute("style").cssText;
<!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>
<title>test </title>
</head>
<body>
<div id="cc" style="color: Black; font-size: 34px;">klkjlkjlfs </div>
</body> <script language="javascript" type="text/javascript">
var i=document.getElementById("cc").getAttribute("style").cssText;
alert(i);
</script>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div id="cc" style="color: Black; font-size: 34px;">klkjlkjlfs</div>
<input type="button" value="test" onclick="test();" />
</body><script language="javascript" type="text/javascript">
function test()
{
//var i=document.getElementById("cc").getAttributeNode("style").nodeValue;
var i=document.getElementById("cc").getAttribute("style","","").cssText;
var arr=i.split(";");
for(var j=0;j<arr.length;j++)
{
var arr1=arr[j].split(":");
alert("key:" + arr1[0] + " value:" + arr1[1]);
}
}
</script></html>
style是div的一个属性
●每个 HTML 标签是一个元素节点
●包含在 HTML 元素中的文本是文本节点
●每一个 HTML 属性是一个属性节点
●注释属于注释节点
HTML DOM 重量元素的节点信息 元素类型 节点信息
nodeName nodeValue nodeType
元素 标签名称 不可用 1
属性 属性名称 属性值 2
文本 #text 文本 3
注释 8
文档 #document 不可用 9 9楼是你的错了吧,属性元素也是元素,为什么不能用nodeValue呢
JS中读取style的文本内容得用 cssText , 因为其内容有很多而且是允许有一些非字母数字的字符,所以设置一个比较少用的属性来获取,nodeValue 规定是获得到节点的值,而不是属性的值,不能混淆了
一般在解析xml文档中会使用到
var i=document.getElementById("cc").getAttributeNode("id").nodeValue;
alert(i);这个就可以读到值,这又是怎么解释呢?
这个在不同IE中不尽相同,比如:
1,
var i=document.getElementById("cc").getAttribute("style").cssText;
在IE 6下可以得到其style属性值, 但在FF,NS IE下为undefined2,
var i=document.getElementById("cc").getAttribute("style");
在IE 6下可以得到style这个属性对象,但得不到其值, 但在FF,NS IE下却可以得到style属性的值3,
但有一点 .getAttribute("属性名称") 在MS,FF,NS IE下都可以用,其符合W3C标准
IE7、IE6获得为空,IE8、FF可以获得其值。http://www.quirksmode.org/dom/w3c_core.html
getAttributeNode()Get an attribute node Test page No Almost Yes Yes Yes Yes Yes Yes
x.getAttributeNode('align')Get the attribute object align of node x. This is an object, not a value.•IE 6/7 don't allow you to access the value of x.getAttributeNode('style').
测试的结果就是答案。给不给理由执行的结果都一样。谁叫IE6、IE7还占领广大市场,你就得兼容。
注意名称观察:getAttribute 和 getAttributeNode 的区别,方法不同,功能不同;前者强调的是属性,后者可以把属性(估计是被转换后)当成节点来操作
elemment.style.cssText是style的文本模式
elemment.getAttributeNode("style")只是把elemment.style封装成了node而已