基于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 相关文章推荐
js汉字转拼音实现代码
Feb 06 Javascript
js获得地址栏?问号后参数的方法
Aug 08 Javascript
JS正则表达式大全(整理详细且实用)
Nov 14 Javascript
javascript实现随时变化着的背景颜色
Apr 02 Javascript
JavaScript正则表达式之multiline属性的应用
Jun 16 Javascript
JavaScipt选取文档元素的方法(推荐)
Aug 05 Javascript
快速掌握jQuery插件开发
Jan 19 Javascript
详解Angular.js数据绑定时自动转义html标签及内容
Mar 30 Javascript
实现图片首尾平滑轮播(JS原生方法—节流)
Oct 17 Javascript
Vue.js实现数据响应的方法
Aug 13 Javascript
jQuery md5加密插件jQuery.md5.js用法示例
Aug 24 jQuery
微信小程序自定义组件的实现方法及自定义组件与页面间的数据传递问题
Oct 09 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
基于mysql的论坛(7)
2006/10/09 PHP
一周学会PHP(视频)Http下载
2006/12/12 PHP
UTF8编码内的繁简转换的PHP类
2009/07/09 PHP
PHP swfupload图片上传的实例代码
2013/09/30 PHP
ThinkPHP模板中数组循环实例
2014/10/30 PHP
Laravel实现autoload方法详解
2017/05/07 PHP
JavaScript建立一个语法高亮输入框实现思路
2013/02/26 Javascript
JS实现日期加减的方法
2013/11/29 Javascript
jquery实现点击弹出层效果的简单实例
2014/03/03 Javascript
改变状态栏文字的js代码
2014/06/13 Javascript
z-blog SyntaxHighlighter 长代码无法换行解决办法(jquery)
2014/11/16 Javascript
js漂浮广告实现代码
2015/08/15 Javascript
jquery实现带缩略图的可定制高度画廊效果(5种)
2015/08/28 Javascript
JavaScript_ECMA5数组新特性详解
2016/06/12 Javascript
JS在Chrome浏览器中showModalDialog函数返回值为undefined的解决方法
2016/08/03 Javascript
jQuery实现微信长按识别二维码功能
2016/08/26 Javascript
JS获取鼠标相对位置的方法
2016/09/20 Javascript
Javascript调试之console对象——你不知道的一些小技巧
2017/07/10 Javascript
利用vue + element实现表格分页和前端搜索的方法
2017/12/25 Javascript
vue 页面加载进度条组件实例
2018/02/05 Javascript
react redux入门示例
2018/04/19 Javascript
[01:03:18]DOTA2-DPC中国联赛 正赛 RNG vs Dynasty BO3 第一场 1月29日
2021/03/11 DOTA
Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
2013/12/04 Python
跟老齐学Python之使用Python查询更新数据库
2014/11/25 Python
使用Python生成随机密码的示例分享
2016/02/18 Python
python中解析json格式文件的方法示例
2017/05/03 Python
python数据抓取分析的示例代码(python + mongodb)
2017/12/25 Python
OpenCV里的imshow()和Matplotlib.pyplot的imshow()的实现
2019/11/25 Python
python3格式化字符串 f-string的高级用法(推荐)
2020/03/04 Python
python安装和pycharm环境搭建设置方法
2020/05/27 Python
June Jacobs尊积帕官网:知名的spa水疗护肤品牌
2019/03/21 全球购物
银行工作检查书范文
2014/01/31 职场文书
财务科长个人对照检查材料
2014/09/18 职场文书
离婚财产处理协议书
2014/09/30 职场文书
2016年清明节红领巾广播稿
2015/12/17 职场文书
各国货币符号大全
2022/02/17 杂记