jquery+遍历+html+list,jQuery遍历

根据标题显示相应的内容,即实现遍历

1c77b01845159d2c246f72130e65e96c.png

实现功能:

鼠标经过时,标题可以发生颜色的变化,下面显示的内容变为该标题所对应的内容。

实现代码:

1.HTML:

列表1
列表2
列表3
列表4
列表5
列表6
列表7
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈1
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈2
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈3
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈4
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈5
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈6
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈7

2.css:

*{

padding:0;

margin:0;

}

.dl{

position:fixed;

700px;

}

.List{

100px;

height:40px;

background-color:pink;

float:left;

cursor:pointer;

text-align:center;

}

.Content{

100%;

height:200px;

background-color:brown;

color:white;

}

.active{

background-color:red;

}

.hide{

display:none;

}

3.js:

在写js代码之前需要引进你本地上的jQuery,也就是js库。

$("document").ready(function(){

$(".List").mouseover(function(){

$(this).addClass("active").siblings().removeClass("active");

var v = $(this).index();

$(".Content").eq(v).removeClass("hide").siblings().addClass("hide");

});

});

注意:

通过给当前元素增加/去除类,同时给自己的同胞元素去除/增加类来完成。其实是实现了css与js的分离,css负责样式的实现,js负责给元素分配相应的类。

完整代码:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

Title

}.dl{position:fixed;width:700px;

}.List{width:100px;height:40px;background-color:pink;float:left;cursor:pointer;text-align:center;

}.Content{width:100%;height:200px;background-color:brown;color:white;

}.active{background-color:red;

}.hide{display:none;

}

$(".List").mouseover(function(){

$(this).addClass("active").siblings().removeClass("active");varv=$(this).index();

$(".Content").eq(v).removeClass("hide").siblings().addClass("hide");

});

});

列表1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值