好友列表选中

当选中一个人时,该背景颜色改变,移动鼠标,鼠标移上变色,离开变回。

  <style type="text/css">
    *{ margin:0px auto; padding:0px; font-family:"微软雅黑";}
    #wai{ width:100%; height:300px; margin-top:20px;}
    #names{ width:300px; height:300px;}
    .name{ width:300px; height:50px; background-color:#9FF; text-align:center; line-height:50px; vertical-align:middle; margin-bottom:10px;}
  </style>
</head>

<body>
  <div id="wai">
    <div id="names">
      <div class="name" sx="0">付一</div>
      <div class="name" sx="0">赵三</div>
      <div class="name" sx="0">李四</div>
      <div class="name" sx="0">宋五</div>
      <div class="name" sx="0">仲六</div>
    </div>
  </div>
</body>
<script type="text/javascript">
  var n = document.getElementsByClassName("name");
  //点击变色
  for(var i=0; i<n.length; i++){
    n[i].onclick=function(){
      for(var i=0; i<n.length; i++){
          n[i].style.backgroundColor="#9FF";
          n[i].setAttribute("sx","0");
          }
      this.style.backgroundColor="red";
      this.setAttribute("sx","1");
      }
  }

  //移动变色
  for(var i=0; i<n.length; i++){
      n[i].onmouseover=function(){
              this.style.backgroundColor="red";
          }
      }

  //离开变回
  for(var i=0; i<n.length; i++){
      n[i].onmouseout=function(){
          var sx = parseInt(this.getAttribute("sx"));
          if(sx==1){
            this.style.backgroundColor="red";
            }
          else if(sx==0){
            this.style.backgroundColor="#9FF";
            }
      }
  }

</script>
</html>

这道题需要自定义一个属性,当属性值为0时,背景色为原来的颜色,当属性值变为1,背景色变色。

这道题有一种不用JS就可以实现的简单方法:

  <style type="text/css">
    *{ margin:0px auto; padding:0px; font-family:"微软雅黑";}
    #wai{ width:100%; height:300px; margin-top:20px;}
    #names{ width:300px; height:300px;}
    .name{ width:300px; height:50px; background-color:#9FF; text-align:center; line-height:50px; vertical-align:middle; margin-bottom:10px;}
    .name:hover{ background-color:red;!important}
    .name:focus{ background-color:red;}
  </style>
</head>

<body>
  <div id="wai">
    <div id="names">
      <div class="name" tabindex="1">付一</div>
      <div class="name" tabindex="1">赵三</div>
      <div class="name" tabindex="1">李四</div>
      <div class="name" tabindex="1">宋五</div>
      <div class="name" tabindex="1">仲六</div>
    </div>
  </div>
</body>

在样式中使用“focus”,它的效果同JS里的“onclick”相似,点击时发生的变化,不过这种属性不能直接用在<div>标签上,需要在标签内加入“tabindex”属性才能使用。

时间: 2024-08-30 17:39:38

好友列表选中的相关文章

鼠标事件和表单事件+好友列表选中效果+侧面菜单下拉效果

通用:onclick 鼠标单击ondblclick 鼠标双击onmouseover 鼠标放上onmouseout 鼠标离开onmousemove 鼠标移动 表单:onchang 表单的值改变onblur 失去焦点onfocus 获得焦点onselect 选中 2.好友列表选中效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/

Js效果:图片轮播;选项卡;侧面菜单下拉效果;进度条效果;滑动效果;好友列表选中效果;点击选中显示效果

选项卡效果表: <body><div id="aaa"> <div class="bbb" style="width:50px; height:30px; background-color:#3F0" onclick="Show('d1')">娱乐</div> <div class="bbb" style="width:50px; height

JS实例之列表选中,实现类似好友列表选中效果

1 <head> 2 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 3 <title>无标题文档</title> 4 <style type="text/css"> 5 *{margin:0px auto; padding:0px;} 6 #wai{width:100px; height:110

例子:好友列表选中效果

<style type="text/css"> *{ margin:0px auto; padding:0px; font-family:微软雅黑; font-size:16px;} .f{ width:200px; height:30px; background-color:#63C; color:white; text-align:center; line-height:30px; vertical-align:middle; margin-top:3px} .f:ho

UI进阶 即时通讯之XMPP好友列表、添加好友、获取会话内容、简单聊天

这篇博客的代码是直接在上篇博客的基础上增加的,先给出部分代码,最后会给出能实现简单功能的完整代码. UI进阶 即时通讯之XMPP登录.注册 1.好友列表 初始化好友花名册 1 #pragma mark - 管理好友 2 // 获取管理好友的单例对象 3 XMPPRosterCoreDataStorage *rosterStorage = [XMPPRosterCoreDataStorage sharedInstance]; 4 // 用管理好友的单例对象初始化Roster花名册 5 // 好友操

仿QQ好友列表界面的实现

TableView有2种style:UITableViewStylePlain 和 UITableViewStyleGrouped. 但是QQ好友列表的tableView给人的感觉似乎是2个style效果都有,但是tableView不能实现2种效果同时存在. 其实只是用到了Plain这个style,只是在cell的个数显示上做了个处理(个人见解,希望可以帮到有需要的人.....) 当通讯录那一组的cell的组头视图中的button是普通状态下的时候,并不是不显示cell,而是显示一个没有任何内容

即时通讯之XMPP好友列表、添加好友、获取会话内容、简单聊天

1.好友列表 初始化好友花名册 1 #pragma mark - 管理好友 2 // 获取管理好友的单例对象 3 XMPPRosterCoreDataStorage *rosterStorage = [XMPPRosterCoreDataStorage sharedInstance]; 4 // 用管理好友的单例对象初始化Roster花名册 5 // 好友操作是耗时操作 6 self.xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:r

练习数据库和网页布局(查询好友列表)

学习:查询好友列表 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&quo

JavaScript实现的购物车效果-好友列表效果

JavaScript实现的购物车效果,当然这个效果可以运用在好多地方,比如好友的选择,人力资源模块,计算薪资,人员的选择等等.下面看类似某种购物车的效果图: code: goodsCar.js:这个js写成了一个单独的文件.主要是控制上面的列表显示的. window.onload=function(){ initStore(); }; var goods=["火腿","美女","御姐","火星一日游","跑车&quo