基于jquery的时间段实现代码


Posted in Javascript onAugust 02, 2012

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://demo.3water.com/jslib/jquery/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>
Javascript 相关文章推荐
Jquery AutoComplete自动完成 的使用方法实例
Mar 19 Javascript
js change,propertychange,input事件小议
Dec 20 Javascript
window.print打印指定div实例代码
Dec 13 Javascript
Firefox中使用outerHTML的2种解决方法
Jun 07 Javascript
完美兼容各大浏览器的jQuery仿新浪图文淡入淡出间歇滚动特效
Nov 12 Javascript
JavaScript对象参数的引用传递
Jan 14 Javascript
JS中Eval解析JSON字符串的一个小问题
Feb 21 Javascript
慕课网题目之js实现抽奖系统功能
Sep 19 Javascript
Node.js静态服务器的实现方法
Feb 28 Javascript
微信小程序页面间值传递的两种方法
Nov 26 Javascript
vue-cli 3.x配置跨域代理的实现方法
Apr 12 Javascript
微信公众号开发之微信支付代码记录的实现
Oct 16 Javascript
Javascript this 的一些学习总结
Aug 02 #Javascript
创建公共调用 jQuery Ajax 带返回值
Aug 01 #Javascript
这些年、我收集的JQuery代码小结
Aug 01 #Javascript
JQquery的一些使用心得分享
Aug 01 #Javascript
javascript 兼容所有浏览器的DOM扩展功能
Aug 01 #Javascript
别了 JavaScript中的isXX系列
Aug 01 #Javascript
JS判断元素为数字的奇异写法分享
Aug 01 #Javascript
You might like
php自动获取目录下的模板的代码
2010/08/08 PHP
php不允许用户提交空表单(php空值判断)
2013/11/12 PHP
PHP使用glob函数遍历目录或文件夹的方法
2014/12/16 PHP
php生成excel列名超过26列大于Z时的解决方法
2014/12/29 PHP
php 三大特点:封装,继承,多态
2017/02/19 PHP
PHP实现上传图片到数据库并显示输出的方法
2018/05/31 PHP
PHP实现chrome表单请求数据转换为接口使用的json数据
2021/03/04 PHP
自写简单JS判断是否已经弹出页面
2010/10/20 Javascript
JavaScript高级程序设计(第3版)学习笔记10 再访js对象
2012/10/11 Javascript
javascript制作游戏开发碰撞检测的封装代码
2015/03/31 Javascript
jQuery的bind()方法使用详解
2015/07/15 Javascript
ECHO.js 纯javascript轻量级延迟加载的实例代码
2016/05/24 Javascript
微信小程序 动态的设置图片的高度和宽度详解及实例代码
2017/02/24 Javascript
利用jQuery实现一个简单的表格上下翻页效果
2017/03/14 Javascript
Vue键盘事件用法总结
2017/04/18 Javascript
微信小程序实现皮肤功能(夜间模式)
2017/06/18 Javascript
vue+elementUI实现表格关键字筛选高亮
2020/10/26 Javascript
javascript设计模式之迭代器模式
2020/01/30 Javascript
npm qs模块使用详解
2020/02/07 Javascript
Vue中父子组件的值传递与方法传递
2020/09/28 Javascript
js屏蔽F12审查元素,禁止修改页面代码等实现代码
2020/10/02 Javascript
[46:55]完美世界DOTA2联赛决赛 FTD vs Phoenix 第三场 11.08
2020/11/11 DOTA
在cmd中运行.py文件: python的操作步骤
2018/05/12 Python
利用python如何处理百万条数据(适用java新手)
2018/06/06 Python
Python如何读写字节数据
2020/08/05 Python
安德玛加拿大官网:Under Armour加拿大
2019/10/02 全球购物
CNC数控操作工岗位职责
2013/11/19 职场文书
计算机毕业生自荐信范文
2014/03/23 职场文书
家长会学生演讲稿
2014/04/26 职场文书
房地产推广策划方案
2014/05/19 职场文书
医药公司采购员岗位职责
2014/09/12 职场文书
个人务虚会发言材料
2014/10/20 职场文书
财务人员廉洁自律心得体会
2016/01/13 职场文书
Html分层的box-shadow效果的示例代码
2021/03/30 HTML / CSS
java设计模式--建造者模式详解
2021/07/21 Java/Android
SQL Server数据库基本概念、组成、常用对象与约束
2022/03/20 SQL Server