JavaScript Attribute用法

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title> attribute</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style type="text/css">
#box{
width:100px;
height:100px;
background: green;

}
</style>
</head>
<body>
<div id="box" style='' bbb='helloworld' >div</div>
<script type="text/javascript">
/*
[]在属性中的运用

obj['style']['height'] ='250px';

var str = oBut.innerHTML;
obj.style[str] ='250px';

属性值在不确定的时候采用[]

attribute节点属性
attribute优势:可以操作.还有[]操作不了的非法的HTML属性
obj.getAttribute(name) 获取节点的值
obj.setAttribute(name,value) 设置节点属性的值
obj.hasAttribute(name) 是否有name属性,true=>有,false=无
obj.removeAttribute(name) 移除 name属性
*/
var obj = document.getElementById('box');
//console.log(obj['bbb'])//undefined
//console.log( obj.getAttribute('style') ); //获取节点属性的值
//console.log( obj.setAttribute('ccc',999) ); //设置节点属性的值
//console.log( obj.hasAttribute('bbb') ); //有 true, 没有 false
console.log( obj.removeAttribute('bbb') ); //移除节点属性
</script>
</body>
</html>

转载于:https://www.cnblogs.com/raulfang/p/8660620.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值