详谈getAttribute中的style
<a style=color:blue;cursor:hand onclick=check(this.getAttribute("style"))>遍历style对象</a> ⒈
<a style=color:blue;cursor:hand onclick=alert(this.getAttribute("style").cssText)>style内容</a> ⒉
<div id=demo></div>
<script>
function check(obj){
demo.innerHTML=""
for(ob in obj)
{
demo.innerHTML+=(ob+"="+obj[ob])+"<br>";
}
}
</script>
⒈显示的结果是: ⒉显示的结果是: color:blue;cursor:hand
clear=
posRight=0
backgroundRepeat=
borderTopStyle=
marginTop=
fontVariant=
listStylePosition=
backgroundPositionX=
lineHeight=
scrollbarHighlightColor=
overflowX=
paddingLeft=
borderLeftWidth=
padding=
listStyleType=
borderLeftColor=
display=
textDecorationLineThrough=false
marginBottom=
textKashidaSpace=
borderCollapse=
textDecorationBlink=false
scrollbarFaceColor=
backgroundAttachment=
borderRightStyle=
fontStyle=
textUnderlinePosition=
textIndent=
textDecorationOverline=false
layoutGridMode=
right=
pageBreakAfter=
background=
filter=
borderColor=
posWidth=0
left=
minHeight=
accelerator=false
rubyOverhang=
layoutGrid=
visibility=
verticalAlign=
borderBottomWidth=
scrollbarShadowColor=
textTransform=
lineBreak=
scrollbarArrowColor=
margin=
borderBottomColor=
borderTopWidth=
behavior=
letterSpacing=
layoutFlow=
font=
borderTopColor=
paddingBottom=
whiteSpace=
overflow=
borderBottomStyle=
cssText=CURSOR: hand; COLOR: blue
width=
clip=
cursor=hand
fontSize=
imeMode=
backgroundPosition=
color=blue
paddingRight=
textAutospace=
pageBreakBefore=
direction=
bottom=
fontFamily=
unicodeBidi=
posHeight=0
posBottom=0
borderRightColor=
styleFloat=
textJustify=
backgroundColor=
posTop=0
zIndex=0
borderLeftStyle=
zoom=
listStyleImage=
wordSpacing=
textDecoration=
borderBottom=
layoutGridChar=
tableLayout=
border=
textAlign=
backgroundPositionY=
backgroundImage=
borderWidth=
borderTop=
textJustifyTrim=
scrollbar3dLightColor=
fontWeight=
scrollbarDarkShadowColor=
textAlignLast=
posLeft=0
borderRightWidth=
paddingTop=
wordBreak=
textOverflow=
rubyPosition=
borderStyle=
borderRight=
wordWrap=
position=
overflowY=
textDecorationUnderline=false
layoutGridLine=
top=
textDecorationNone=false
writingMode=
height=
scrollbarTrackColor=
listStyle=
scrollbarBaseColor=
marginRight=
marginLeft=
layoutGridType=
textKashida=
rubyAlign=
borderLeft=
可以看出:obj.getAttribute("style")其实是得到一个style对象,完全等价于这个语句:“obj.style”,其实obj.getAttribute("XXX")<=====>obj.XXX
因为style不象Heigh一样,style得到的还是一个style对象(obj),而Height得到的是一个字符串,因此可以直接显示出来!
另外:
obj.attributes.getNamedItem(name).nodeValue====obj.attributes.getNamedItem(name).value
例外:在IE浏览器中:obj.attributes.getNamedItem("style").value==null,除了“style”属性以外,其他的和obj.getAttribute(name)完全一样!
浙公网安备 33010602011771号