getAttribute、setAttribute和removeAttribute

< script>
function fnGet(){
oPersistInput.value=oPersistInput.getAttribute("new");
}
function fnSet(){
oPersistInput.setAttribute("new",oPersistInput.value);
}
function fnRem(){
oPersistInput.removeAttribute("new");
}
< /script>
< input type=text id="oPersistInput">
< input type=button value="getattribute" οnclick="fnGet()">
< input type=button value="setattribute" οnclick="fnSet()">
< input type=button value="removeattribute" οnclick="fnRem()">
这三个方法可以方便的给标签添加、获取和删除属性并给属性赋值。这个代码是从DHTML手册中摘录出来并修改的,可供参考。简单的翻译了一下手册对这三个属性的介绍:


getAttribute语法:


vAttrValue = object.getAttribute(sAttrName [, iFlags])


getAttribute参数:


sAttrName Required. String that specifies the name of the attribute.


(sAttrName参数是必须的,表示属性的名称)


iFlags Optional. Integer that specifies one or more of the following flags: 0 Default. Performs a property search that is not case-sensitive, and returns an interpolated value if the property is found.


1 Performs a case-sensitive property search. To find a match, the uppercase and lowercase letters in sAttrName must exactly match those in the attribute name. If the iFlags parameter for getAttribute is set to 1 and this option is set to 0 (default), the specified property name might not be found.


2 Returns the value exactly as it was set in script or in the source document.


(iFlags是可选参数,默认值为 0。0:执行不考虑字母大小写的搜索,假如特性被找到了返回一个以内插值替换的值。假如 setAttribute 方法的 iFlags 参数设置为 1 而此方法的 iFlags 参数设置为 0 , 则满足 sAttrName 指定的特性名称不一定能被找到。1:执行严格考虑字母大小写的搜索。 2:严格的按照脚本或源文档里的设置返回值。)


setAttribute语法:


object.setAttribute(sName, vValue [, iFlags])


setAttribute参数:


sName Required. String that specifies the name of the attribute.


(sName参数是必须的,表示属性的名称)


vValue Required. Variant that specifies the string, number, or Boolean to assign to the attribute.


(vValue参数是必须的,表示赋给属性的值)


iFlags Optional. Integer that specifies one the following flags: 0 When the attribute is set, it overwrites any attributes with the same name, regardless of their case.


1 Default. The case of the attribute that you set is respected when it is set on the object.


(iFlags是可选参数,默认值为 1。0:属性的值一旦设定便无法被覆盖,名字相同的属性将会被忽略。1:写入时,覆盖已经被设定的属性值。)


removeAttribute语法:


bSuccess = object.removeAttribute(sName [, iCaseSensitive])


removeAttribute参数:


sName Required. String that specifies the attribute name.


(sName参数是必须的,表示属性的名称)


iCaseSensitive Optional. Integer that specifies whether to use a case-sensitive search to locate the attribute. By default, this value is set to 1 to indicate that the uppercase and lowercase letters in the specified sName parameter must exactly match those in the attribute name. If there are multiple attributes specified with different case sensitivity, the attribute returned might vary across platforms.


(iCaseSensitive是可选参数,默认值为 1 。0:在匹配名称时,不区分大小写 1:在匹配名称时,区分大小写。)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值