//1. li.setAttribute(“score”,100);
//a.会在li标签上生成一个score属性
//b.该属性由于不是html的标准属性,所以不会被封装到dom对象中
//c.可以用getAttribute(“score”)方法来获取该属性值,但是由于该属性值不是对象的基本信息,所以无法通过li.score来访问
//2. li.score = 100;
//a.给li这个dom对象添加一个属性值,该信息会被添加到dom对象中
//b.不会在li标签上生成一个score属性
//c.可以通过li.score来访问该属性值,但是不可以用getAttribute(“score”)方法来获取该属性值,因为这个属性不在标签上
//3.如果要给一个dom对象添加自定义的数据,建议直接使用点的方式,因为使用setAttribute()会在标签上生成额外的属性节点
案例:tab键切换显示
<div class="box" id="box"><div class="hd"><span class="current">体育</span><span>娱乐</span><span>新闻</span><span>综合</span></div><div class="bd"><ul><li class="current">我是体育模块</li><li>我是娱乐模块</li><li>我是新闻模块</li><li>我是综合模块</li></ul></div></div><style>* {margin: 0;padding: 0;}ul {list-style-type: none;}.box {width: 400px;height: 300px;border: 1px solid #ccc;margin: 100px auto;overflow: hidden;}.hd {height: 45px;}.hd span {display: inline-block;width: 90px;background-color: pink;line-height: 45px;text-align: center;cursor: pointer;}.hd span.current {background-color: purple;}.bd li {height: 255px;background-color: purple;display: none;}.bd li.current {display: block;}</style><script>//获取最外面的divvar box=my$("box");//获取的是里面的第一个divvar hd=box.getElementsByTagName("div")[0];//获取的是里面的第二个divvar bd=box.getElementsByTagName("div")[1];//获取所有的li标签var list=bd.getElementsByTagName("li");//=================================//获取所有的span标签var spans=hd.getElementsByTagName("span");//循环遍历的方式,添加点击事件for(var i=0;i<spans.length;i++){//在点击之前就把索引保存在span标签中spans[i].setAttribute("index",i);//================================spans[i].onclick=function () {//第一件事,所有的span的类样式全部移除for(var j=0;j<spans.length;j++){spans[j].removeAttribute("class");}//第二件事,当前被点击的span应用类样式this.className="current";//span被点击的时候获取存储的索引值//alert(this.getAttribute("index"));var num=this.getAttribute("index");//==============================//获取所有的li标签,每个li标签先全部隐藏for(var k=0;k<list.length;k++){list[k].removeAttribute("class");}//当前被点击的span对应的li标签显示list[num].className="current";};}</script>

