jquery 时间段

简介: 首先,看下效果图: json字符串:var mcode={"minfo":[{"time":"9:00-10:00","status":2},{"time":"10:00-11:00","status":1},{"time":"11:00-12:00","status":3},{"time":"1...

首先,看下效果图:


json字符串:
var mcode={"minfo":[{"time":"9:00-10:00","status":2},{"time":"10:00-11:00","status":1},{"time":"11:00-12:00","status":3},{"time":"13:00-14:00","status":1},{"time":"14:00-15:00","status":1},{"time":"15:00-16:00","status":1},{"time":"16:00-17:00","status":1},{"time":"17:00-18:00","status":1}]};
其中time代表时间段,status当职位1时代表可以使用,2时代表已过期,3时代表已选满。
通过循环遍历json字符串中的数据值。

for(var i in mcode.minfo){
       mcode.minfo[i].time + mcode.minfo[i].status;
}


当前时间段为已过期或以选满时,鼠标移动到其当前时间段上时提示相应信息,鼠标移开取消提示。
当前时间段为橘黄色代表可以选择。

$.each($("#test span"),function(k,v){
     if($(this).hasClass("unspan1")||$(this).hasClass("unspan2")){
          $(this).hover(function(){
               $(this).find("label").css({"display":"block"});
               $(this).find("em").css({"display":"block"});  
          }, function(){
               $(this).find("label").css({"display":"none"});
               $(this).find("em").css({"display":"none"}); 
          }); 
     }
      else{
          $(this).click(function(){
            $("#result").empty().html("您选择了:"+$(this).text());
          });
      }
  });

 

拼接字符串,构建html结构。

for(var i in mcode.minfo){
    if(mcode.minfo[i].status===2){
        html+='<span class="unspan1 ';
    }
    else if(mcode.minfo[i].status===3){
         html+='<span class="unspan2 ';
    }
    else{
         html+='<span class=" ';
    }
    if((i+1)%3===0){
         html+='" >';
    }
    else{
         html+='mspan" >';   
    }
    html+=mcode.minfo[i].time;
         if(mcode.minfo[i].status===2){
              html+='<label>已过期</label>';
         }
         else if(mcode.minfo[i].status===3){
              html+='<label>已选满</label>';
         }
         if(mcode.minfo[i].status!==1){
              html+='<em></em>';
         }
    html+="</span>";
 }

 

css样式:

#test{ width: 430px; padding: 35px; border: 1px solid #666666;overflow: hidden; margin: 100px auto 0px;}
#test span{display:block; background: #FF6600; width:130px; height: 30px; line-height: 30px; text-align: center; float:left; 
_display
:inline; position:relative; margin-bottom: 15px; cursor: pointer;} #test .mspan{margin-right: 20px;} #test .unspan1{background: #D2E0E6; cursor:default} #test .unspan2{background: #ffcaca; cursor: default;} #test label{position: absolute; left:25px; top:-18px; width: 60px; line-height: 20px; background: #F3F3F3;
padding
:1px 10px; border:1px solid #CCCCCC;display: none;} #test em{display: block;border-color: #F3F3F3 transparent transparent transparent;border-style: solid;border-width: 6px 6px 6px 6px; padding: 0;width: 0;height: 0; font-size: 0;line-height: 0; position: absolute;left:58px; top:5px;display:none; _border-top-color: #F3F3F3;_border-bottom-color: #F3F3F3; _filter: chroma( color = #F3F3F3); } #result{ margin: 10px auto 0px; text-align: center}



实例:

<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="http://files.cnblogs.com/kuikui/jquery.js"></script> <style type="text/css"> *{margin:0px;padding: 0px;} #test{ width: 430px; padding: 35px; border: 1px solid #666666;overflow: hidden; margin: 100px auto 0px;} #test span{display:block; background: #FF6600; width:130px; height: 30px; line-height: 30px; text-align: center; float:left; _display:inline; position:relative; margin-bottom: 15px; cursor: pointer;} #test .mspan{margin-right: 20px;} #test .unspan1{background: #D2E0E6; cursor:default} #test .unspan2{background: #ffcaca; cursor: default;} #test label{position: absolute; left:25px; top:-18px; width: 60px; line-height: 20px; background: #F3F3F3; padding:1px 10px; border:1px solid #CCCCCC;display: none;} #test em{display: block;border-color: #F3F3F3 transparent transparent transparent;border-style: solid;border-width: 6px 6px 6px 6px; padding: 0;width: 0;height: 0; font-size: 0;line-height: 0; position: absolute;left:58px; top:5px;display:none; _border-top-color: #F3F3F3;_border-bottom-color: #F3F3F3; _filter: chroma( color = #F3F3F3); } #result{ margin: 10px auto 0px; text-align: center} </style> </head> <body> <div id="test"> </div> <div id="result"></div> <script type="text/javascript"> var mcode = { "minfo": [ { "time": "9:00-10:00", "status": 2 }, { "time": "10:00-11:00", "status": 1 }, { "time": "11:00-12:00", "status": 3 }, { "time": "13:00-14:00", "status": 1 }, { "time": "14:00-15:00", "status": 1 }, { "time": "15:00-16:00", "status": 1 }, { "time": "16:00-17:00", "status": 1 }, { "time": "17:00-18:00", "status": 1 } ] }; var html = ''; for(var i in mcode.minfo){ if(mcode.minfo[i].status===2){ html+='<span class="unspan1 '; } else if(mcode.minfo[i].status===3){ html+='<span class="unspan2 '; } else{ html+='<span class=" '; } if((i+1)%3===0){ html+='" >'; } else{ html+='mspan" >'; } html+=mcode.minfo[i].time; if(mcode.minfo[i].status===2){ html+='<label>已过期</label>'; } else if(mcode.minfo[i].status===3){ html+='<label>已选满</label>'; } if(mcode.minfo[i].status!==1){ html+='<em></em>'; } html+="</span>"; } $("#test").empty().html(html); $.each($("#test span"),function(k,v){ if($(this).hasClass("unspan1")||$(this).hasClass("unspan2")){ $(this).hover(function(){ $(this).find("label").css({"display":"block"}); $(this).find("em").css({"display":"block"}); }, function(){ $(this).find("label").css({"display":"none"}); $(this).find("em").css({"display":"none"}); }); } else{ $(this).click(function(){ $("#result").empty().html("您选择了:"+$(this).text()); }); } }); </script> </body> </html>

目录
相关文章
|
编译器 程序员 C++
2023-4-6-C++11、C++14、C++17、C++20版本新特性系统全面的学习!(二)
2023-4-6-C++11、C++14、C++17、C++20版本新特性系统全面的学习!
706 0
2023-4-6-C++11、C++14、C++17、C++20版本新特性系统全面的学习!(二)
|
10月前
|
JSON 人工智能 前端开发
前端开发中使用whistle代理工具
Whistle是一款强大的代理工具,相比Charles、Fiddler更轻量且功能丰富。它适用于前端开发中的多种场景,如接口数据Mock、接口代理、静态资源代理等。通过简单的规则配置,可将接口指向本地JSON文件,解决跨域问题,或代理静态资源以满足特定域名访问需求。此外,Whistle还支持本地端口间转发与移动端请求抓包,搭配SwitchyOmega插件使用效果更佳。需注意,使用前请确保已安装Node环境并参考官方文档完成基础配置。
|
11月前
|
机器学习/深度学习 人工智能 搜索推荐
《探秘AI驱动的个性化推荐系统:精准触达用户的科技密码》
在这个信息爆炸的时代,AI驱动的个性化推荐系统应运而生,通过数据收集与处理、构建用户画像、核心算法(协同过滤与基于内容的推荐)及深度学习技术,精准洞察用户需求。它广泛应用于电商、视频平台等领域,提升用户体验和商业效益。尽管面临数据稀疏性、隐私保护等挑战,未来将更加精准、实时并注重用户隐私。
921 1
《探秘AI驱动的个性化推荐系统:精准触达用户的科技密码》
|
运维 网络安全 持续交付
IDEA+Docker 远程一键部署项目:技术干货分享
【10月更文挑战第4天】在现代软件开发中,快速、可靠、自动化的部署流程是提升开发效率和运维质量的关键。IDEA(IntelliJ IDEA)作为Java开发者首选的IDE,结合Docker这一轻量级容器化技术,能够实现远程一键部署项目,极大地简化了开发到生产的流程。今天,我将和大家分享这一组合在工作学习中的实际应用和技术干货。
1124 3
|
安全 编译器 C语言
一文讲清楚内联函数 inline
在C语言中,如果一些函数被频繁调用,不断地有函数入栈,即函数栈,会造成栈空间或栈内存的大量消耗。 为了解决这个问题,特别的引入了inline修饰符,表示为内联函数。
|
消息中间件 小程序 Java
暹罗点餐开源啦,一款java多门店点餐系统-连锁门店如蜜雪冰城瑞幸咖啡
暹罗点餐是一款Java餐饮点餐系统,适用于多门店的连锁品牌,对标蜜雪冰城、瑞幸咖啡。系统包含用户端、商家端、配送端以及总管理后台; * 前端使用uni-app开发,可打包部署到微信小程序、APP、H5 * Web端使用vue + Element开发 * 服务端使用java语言开发,技术栈:Spring Boot + Redis + RocketMQ + WebSocket + ElasticSearch + ELK + SpringBoot Admin
1191 1
暹罗点餐开源啦,一款java多门店点餐系统-连锁门店如蜜雪冰城瑞幸咖啡
|
缓存 JavaScript 测试技术
Vue 3实战:打造交互丰富的任务管理应用
Vue 3实战:打造交互丰富的任务管理应用
394 0
|
编解码
一文带你了解 嵌入式Typec 接口切换开关
一文带你了解 嵌入式Typec 接口切换开关
461 0
|
分布式计算 Hadoop Java
hbase_异常_05_End of File Exception between local host is: "rayner/127.0.1.1"; destination host is: "localhost":9000;
一、异常信息 java.io.EOFException: End of File Exception between local host is: "ubuntu/127.0.1.1"; destination host is: "localhost":9000; : java.
4702 0
|
消息中间件 缓存 Kafka
Flink之水位线(Watermark)1
Flink之水位线(Watermark)
335 0