html5 自定义标签 注解

<!DOCTYPE html> 
<html> 
<head> 
<script> 
/*html5 自定义标签的使用 html5自定义标签只允许 以 data- 为开头 定义*/  
/*functions here*/  
function getElementAttribute(elemID) {  
var theElement = document.getElementById(elemID);  
// 赋值开始
/*js方式给自定义元素赋值*/  
theElement.setAttribute('data-product-category','sale');
//数据集方式给自定义元素赋值
//theElement.dataset.productCategory = "sale";    
//赋值结束
//取值开始
var theAttribute = theElement.getAttribute('data-product-category');  
alert(theAttribute);
//数据集的取值方式为驼峰式 如 data-produce-lxx 取值的元素就转换为 productLxx
var theAttribute1 = theElement.dataset.productLxx; 
alert(theAttribute1);
var theAttribute2 = theElement.dataset.product; 
alert(theAttribute2);
var theAttribute3 = theElement.dataset.productLxxLxx; 
alert(theAttribute3);
//取值结束  
//删除元素开始
theElement.removeAttribute('data-product-category'); 
//theElement.dataset.productCategory = null; 
var theAttribute = theElement.getAttribute('data-product-category');  
alert(theAttribute); 
//删除元素结束


//最后目前对数据集操作自定义标签方式各个浏览器支持情况不一 所以考虑兼容性 建议还是使用 dom方式





</script> 
</head> 
<body> 
<div id="product1" data-product-category="clothing" data-product-lxx="hello" data-product="lxx"  data-product-lxx-lxx="h,h"> 
药 ,药 ,彻可闹 ~~ 
</div> 
<input type="button" value="get attribute" οnclick="getElementAttribute('product1')"/> 


</body> 
</html> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值