基于jquery的时间段实现代码_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":"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.jb51.net/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>

时间: 2024-12-12 17:23:23

基于jquery的时间段实现代码_jquery的相关文章

基于JQUERY的多级联动代码_jquery

jquery.select.more.js 复制代码 代码如下: (function($){ $.fn.doselectmore = function(settings) { var dfop ={ namekey: "name", pnamekey: "name", idkey: "id", selectname:"sel", method: "POST", datatype: "json&qu

基于jQuery的倒计时插件代码_jquery

剩余时间:1小时:40分:30秒 复制代码 代码如下: 1 /* * 倒计时插件,主要用来限时购买 * By wayshan 版本1.0 * 使用方法: * $(function(){ * $("#ElementId").countdown({ * Edate:"2012-12-21 15:14:23" * }); * }) */ ;(function($){ $.fn.countdown = function(options){ if (this.length =

基于jquery的Repeater实现代码_jquery

如何实现一个js版的repeater? Asp.net WebForm的repeater控件挺好用,我想用js实现一个在Ajax应用中也该还不错!半年前做了一个jQuery.Repeater插件,并用在了一个项目中,如今拿来晒晒! 原理 项模板为HTML代码,插件接收json数据源,读取模板并创新每一项. 模板HTML 复制代码 代码如下: <ul id='repeater1'> <li class='itemtempplate'>{列名}</li> </ul&g

基于jQuery的倒计时实现代码_jquery

在线演示:http://demo.jb51.net/js/2012/mydaojishi/打包下载:mydaojishi_jb51核心代码: 复制代码 代码如下: $(function(){ var tYear = ""; //输入的年份 var tMonth = ""; //输入的月份 var tDate = ""; //输入的日期 var iRemain = ""; //开始和结束之间相差的毫秒数 var sDate =

基于jQuery的js分页代码_jquery

复制代码 代码如下: function pagerBar(dataCount,pageSize,serverUrl,contentPlace,pagerbarPlace,callBack){ this.dataCount = dataCount; this.pageSize = pageSize; this.serverUrl = serverUrl; this.contentPlace = $("#"+contentPlace); this.pagerbarPlace = $(&qu

基于jquery的动画效果代码_jquery

jQuery基于ajax实现星星评论代码_jquery

本文实例讲述了jQuery基于ajax实现星星评论代码.分享给大家供大家参考.具体如下: 这里使用jquery模仿点评网的星星评论功能,Ajax评论模块,鼠标点击星星即可评价,下边是分数,可以点击后给分,网上很流行的效果,本代码相对完整,相信很多朋友会喜欢的. 先来看看运行效果截图: 具体代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/

基于json的jquery地区联动效果代码_jquery

写这个东西的初衷是来之于新浪微博,在新浪微博帐号设置里面有个地区的选项,使用js写的,想把它的代码给截获下来,可是失望的是它的js代码压缩了,不过看到的里面json类型格式设计的挺好的,一般我们后台未做任何处理的json数据格式类似以下这样子的 [{"Code":3231,"Name":"长春市"}, {"Code":3232,"Name":"吉林市}] 如果有一万个地区,会多出几万个字符,这么奢

基于jquery实现省市联动特效_jquery

本文实例讲述了基于jquery实现省市联动特效的代码,分享给大家供大家参考,具体如下: 运行效果图: 具体代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Demo</title> <!-- 引入jquery --> <script src="http://lib.si