语法:
vAttrValue = object . getAttribute ( sAttrName , iFlags )
参数:
sAttrName : 必选项。字符串(String)。指定属性的名称。
iFlags : 可选项。整数值(Integer)。0 | 1 | 2 0 : 默认值。执行不考虑字母大小写的搜索,假如特性被找到了返回一个以内插值替换的值。假如 setAttribute 方法的 iFlags 参数设置为 1 而此方法的 iFlags 参数设置为 0 , 则满足 sAttrName 指定的特性名称不一定能被找到。
1 : 执行严格考虑字母大小写的搜索。
2 : 严格的按照脚本或源文档里的设置返回值。
返回值:
vAttrValue : 返回属性的值。可能是任意类型的变量。假如属性没有被呈递,则返回 null 。
说明:
获取指定属性的值。
假如对象有多个名字一样但字母大小写不同的属性,并且 iFlags 参数被设为 0 ,那么只有其中最后被建立的那一个会被此方法获取。而其他的则会被忽略。
当使用此方法获取 CLASS 属性的值时,需要将 sName 参数指定为 className 。这是 CLASS 属性所对应的 DHTML 特性。
此方法仅仅可以由从 HTML 组件建立的事件使用。
<script>
var oCodeDiv;
var oFont;
var oSel;
function window.onload(){
oCodeDiv=document.all("idCodeDiv");
oFont=document.all("idFont");
oSel=document.all("idSel");
for (m=0;m<oFont.attributes.length;m++) {
if (oFont.attributes[m].specified){
var oOption=document.createElement("option");
oSel.options.add(oOption,1);
with (oOption) {value=innerText=oFont.attributes[m].nodeName}
}
}
oFont.title=oCodeDiv.innerText=oFont.outerHTML;
}
function doGetAttribute(){
with (oSel) {if (options[selectedIndex].value=="") return;
var sAttribute=options[selectedIndex].value;};
oCodeDiv.innerText=sAttribute+" = "+oFont.getAttribute(sAttribute,2);;
}
</script>
<input type=button color=red style="font-weight:bold" name=rdl case=died onmouseover="this.style.color='blue';" onmouseout="this.style.color='black';" id=idFont value="这里有一些文字"></input>
<br><br><br>
<select id="idSel" onchange="doGetAttribute();">
<option value="" style="font-weight:bold;">---请选择属性---
<option value="case" style="font-weight:bold;">case
</select>
<br><br><div id=idCodeDiv></div>
