为什么设置id的属性值要用className,不能直接

为什么设置id的属性值要用className,不能直接

为什么设置id的属性值要用className,不能直接写dot【i】.id="",className代表什么意思

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>轮播图</title>

<link rel="stylesheet" type="text/css" href="css/背景.css">

<link rel="stylesheet" type="text/css" href="css/按钮.css">

<link rel="stylesheet" type="text/css" href="css/圆点.css">

</head>

<body>

<div class="main" id="main">

<div class="backgroud" id="bg">

<a href="">

<div class="pic pic1 "></div>

</a>

<a href="">

<div class=" pic pic2 dpy"></div>

</a>

<a href="">

<div class=" pic pic3"></div>

</a>

</div>

<a href="" class="button back" id="back"></a>

<a href="" class="button next" id="next"></a>

<div class="dot" id="dot">

<span  ></span>

            <span></span>

            <span></span>

</div>

</div>

<!--  <script type="text/javascript" src="js/javascript.js"></script> -->

<script type="text/javascript" src="js/js1.js"></script>

</body>

</html>

*{

padding: 0px;

margin: 0px;

}

body,html{

padding: 0px;

margin: 0px;

}

.main{

width: 1200px;

height: 460px;

margin: 30px auto;

overflow: hidden;

position: relative;

}

.main .backgroud{

width: 1200px;

height: 460px;

overflow: hidden;   /*减掉移除的图片部分*/

position: relative;


}

.main .backgroud a .pic{

background-repeat: no-repeat;

width: 1200px;

height: 460px;

position: absolute;

display: none;

}

.main .backgroud a .pic1{

background-image: url(../img/bg1.jpg);


}

.main .backgroud a .pic2{

background-image: url(../img/bg2.jpg);


}

.main .backgroud a .pic3{

background-image: url(../img/bg3.jpg);

}

.main .backgroud a .dpy{

display: block;

}

.main .button{

width: 40px;

height:80px;

position: absolute;

left: 244px;

top: 50%;

margin-top: -40px;

background: url(../img/arrow.png) no-repeat center center;

}

.main .button:hover{

background-color: black;

opacity: 0.8

}

.main .back{

transform: rotate(180deg);

}

.main .next{

left: auto;

right: 0;

}

.main .dot{

position: absolute;

right: 15px;

bottom: 24px;

}

.main .dot span{

display: inline-block;

width: 12px;

height:12px;

line-height: 12px;

border-radius: 50%;

background: rgba(7,17,27,0.4);

box-shadow: 0 0 0 2px rgba(255,255,255,0.8) inset;  /* 设置描边*/

margin-left: 20px;

cursor:pointer;

}

.main .dot .now{

     box-shadow: 0 0 0 2px rgba(7,17,27,0.4) inset;

     background-color: white;

}

function byId(id) {

return typeof(id)==="string"?document.getElementById(id):id;

var timer=null;

var index=0;

var main=byId('main');

var pics=byId(bg).getElementsByTagName("div");

var len=pics.length;

var dot=byId('dot').getElementsByTagName('span');

function changepic(){

showPic();

main.onmouseover=function(){

if(timer){

clearInterval(timer);

}

}

main.onmouseout=function()

{

timer=setInterval(function(){

index++;

if(index>=len){

index=0;

}

console.log(index);

showPic()

},3000);

}

    main.onmouseout();

    for(var d=0;d<len;d++){

    dot[d].id=d;

    dot[d].onclick=function(){

    index=this.id;

        howPic();

    }

    }

}

  function  showPic(){

  for(var i=0;i<len;i++){

  pics[i].style.display="none";

  dot[i].className="";

  }

  pics[index].style.display="block";

  dot[index].className="now";

  }

  changepic();

http://img1.sycdn.imooc.com//climg/5c90fd7e000170b506550256.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿莹 2019-03-20 10:22:21

dot[i].className=""; 意思是说,把这个坐标是 i 的这个span标签的class属性的值设置成 “”

也就是这个状态

http://img1.sycdn.imooc.com//climg/5c91a2d80001bb8d03590111.jpg

把想要让他显示的那张图片显示成:

http://img1.sycdn.imooc.com//climg/5c91a314000198be04620110.jpg

for循环中的是先让这三张都不显示,然后循环外是设置像让它展示的那种张显示,比如给它的class一个属性值,比如就是now,我们给classname 是now的设置样式,并让其显示。

2、关于为什么不能直接设置id=“”:

id是唯一的,不能都设置成“”;

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师