日历 点击input搜索框弹出日历并且选中日期,再点击搜索框隐藏日历

 

 

 

 

 

 

 

打印出选中的日期:

 

 

 

1.   <input class="input-item0" type="text" :value="searchDate" placeholder="2019年3月29日" @click="open">

 

2.    <uni-popup :show="dateCheck" @hidePopup="open" class="date">

     <button class="calendar-button-confirm btn1" @click="confirm">确认</button>
      <button class="calendar-button-confirm btn2" @click="open">取消</button>

 

3.  return {
                searchDate: "",
                dateCheck: false,

4.  

            open() {
                this.dateCheck = !this.dateCheck;
                if (this.dateCheck) {

                    if (this.tags[3].checked) {
                        this.slide = 'horizontal'
                    } else if (this.tags[2].checked) {
                        this.slide = 'vertical'
                    } else {
                        this.slide = 'none'
                    }
                    if (this.tags[4].checked) {
                        this.startDate = this.tags[4].value
                    } else {
                        this.startDate = ''
                    }
                    if (this.tags[5].checked) {
                        this.endDate = this.tags[5].value
                    } else {
                        this.endDate = ''
                    }
                    if (this.tags[7].checked) {
                        this.date = this.tags[7].value
                    } else {
                        this.date = ''
                    }
                    this.show = true
                    console.log(this.date)
                }
            },

 

5.     confirm() {
                this.show = false;
                this.dateCheck = !this.dateCheck;
                this.searchDate = this.timeData.fulldate;
            }

 

<template>
    <view>

          <!-- 顶部分段器 -->
                <!-- <view class="uni-padding-wrap uni-common-mt "  v-show="typeIndex">
                    <uni-segmented-control class="top" :current="current" :values="items" style-type="button"
                     :activeColor="activeColor" @click="onClickItemTop"></uni-segmented-control>
                </view> -->
                <view v-show="typeIndex" class="wei">
                    <view class="weixiu">
                        <image src="../../static/images/2级/1巡检任务/3级-巡检记录/Button-02.png"></image>
                        <text class="t t1">报修中</text>
                    </view>
                    <view class="weixiu">
                        <image src="../../static/images/2级/1巡检任务/3级-巡检记录/Button-03.png"></image>
                        <text class="t">已处理</text>
                    </view>
                </view>
         <!-- 搜索框 -->
       <!-- <view class="input-view" v-show="!typeIndex">
            <uni-icon type="search" size="22" color="#666666"></uni-icon>
            <input confirm-type="search" class="input" type="text" placeholder="输入搜索关键词" />
        </view> -->
        
        <view class="input-item" v-show="!typeIndex">
            <!-- <button class="calendar-button" type="button" @click="open">打开日历</button> -->
             <input class="input-item0" :value="searchDate" type="text" placeholder="2019年3月29日"  @click="open" >
             <image src="../../static/images/2级/1巡检任务/3级-巡检记录/search.png" class="input-img1"></image>
             <image src="../../static/images/1级/x.png" class="input-img2"></image>
             <image src="../../static/images/2级/1巡检任务/3级-巡检记录/date.png" class="input-img3"></image>
             <text class="input-text">取消</text>
         </view>
        
        <!-- 下边分段器 -->
        <view class="uni-padding-wrap uni-common-mt tab">
            <uni-segmented-control :current="current" :values="searchTypes" @clickItem="onClickItem" style-type="text"
             :activeColor="activeColor" class="header"></uni-segmented-control>
        </view>
        

        <view class="container">
            

            <view class="content">
                <!-- 报修时间内容 -->
                <view v-show="current === 0" class="current1">


                    <view class="uni-list" @click="showTopPopup">
                        <view class="red-item">
                            <image src="../../static/images/2级/1巡检任务/3级-巡检记录/di-04.png" class="red"></image>
                            <text class="baoxiu">报修中</text>
                        </view>
                        <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in list" :key="key">
                            <view class="uni-media-list uni-pull-right">
                                <view class="uni-media-list-logo">
                                    <image v-if="showImg" :src="value.img" class="list-img"></image>
                                </view>
                                <view class="uni-media-list-body">
                                    <view class="uni-media-list-text-top">{{value.title}}</view>
                                </view>
                            </view>
                        </view>
                    </view>

                    <view class="uni-list" @click="showTopPopup">

                        <view class="red-item">
                            <image src="../../static/images/2级/1巡检任务/3级-巡检记录/di-04.png" class="red"></image>
                            <text class="baoxiu">已处理</text>
                        </view>
                        <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in list" :key="key">
                            <view class="uni-media-list uni-pull-right">
                                <view class="uni-media-list-logo">
                                    <image v-if="showImg" :src="value.img" class="list-img"></image>
                                </view>
                                <view class="uni-media-list-body">
                                    <view class="uni-media-list-text-top">{{value.title}}</view>
                                </view>
                            </view>
                        </view>
                    </view>

                    <view class="uni-list" @click="showTopPopup">

                        <view class="red-item">
                            <image src="../../static/images/2级/1巡检任务/3级-巡检记录/di-04.png" class="red"></image>
                            <text class="baoxiu">报修中</text>
                        </view>
                        <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in list" :key="key">
                            <view class="uni-media-list uni-pull-right">
                                <view class="uni-media-list-logo">
                                    <image v-if="showImg" :src="value.img" class="list-img"></image>
                                </view>
                                <view class="uni-media-list-body">
                                    <view class="uni-media-list-text-top">{{value.title}}</view>
                                </view>
                            </view>
                        </view>
                    </view>

                </view>

                <view v-show="current === 1" class="current2">


                    <view class="uni-list">
                        <view class="red-item">
                            <image src="../../static/images/2级/1巡检任务/3级-巡检记录/di-04.png" class="red"></image>
                            <text class="baoxiu">报修中</text>
                        </view>
                        <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in list" :key="key">
                            <view class="uni-media-list uni-pull-right">
                                <view class="uni-media-list-logo">
                                    <image v-if="showImg" :src="value.img" class="list-img"></image>
                                </view>
                                <view class="uni-media-list-body">
                                    <view class="uni-media-list-text-top">{{value.title}}</view>
                                </view>
                            </view>
                        </view>
                    </view>


                    <view class="uni-list">

                        <view class="red-item">
                            <image src="../../static/images/2级/1巡检任务/3级-巡检记录/di-04.png" class="red"></image>
                            <text class="baoxiu">已处理</text>
                        </view>
                        <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in list" :key="key">
                            <view class="uni-media-list uni-pull-right">
                                <view class="uni-media-list-logo">
                                    <image v-if="showImg" :src="value.img" class="list-img"></image>
                                </view>
                                <view class="uni-media-list-body">
                                    <view class="uni-media-list-text-top">{{value.title}}</view>
                                </view>
                            </view>
                        </view>
                    </view>


                    <view class="uni-list">

                        <view class="red-item">
                            <image src="../../static/images/2级/1巡检任务/3级-巡检记录/di-04.png" class="red"></image>
                            <text class="baoxiu">报修中</text>
                        </view>
                        <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in list" :key="key">
                            <view class="uni-media-list uni-pull-right">
                                <view class="uni-media-list-logo">
                                    <image v-if="showImg" :src="value.img" class="list-img"></image>
                                </view>
                                <view class="uni-media-list-body">
                                    <view class="uni-media-list-text-top">{{value.title}}</view>
                                </view>
                            </view>
                        </view>
                    </view>


                </view>
            </view>

        </view>

        <!-- 居中 -->
        <uni-popup :show="showPopupMiddle" :type="popType" v-on:hidePopup="hidePopup">
          
         
        </uni-popup>
        <!-- 顶部 弹出层 开始-->
        <uni-popup :show="showPopupTop" :type="popType" v-on:hidePopup="hidePopup">
            <view class="pop">
                <view class="pop-title">设备报修情况</view>
                <view class="pop-item1">
                    <view>负责人: 张晓晓</view>
                    <view>联系方式: 13812563210</view>
                    <view>设备编号: MHS2019032500000001</view>
                </view>
                <view class="pop-item2">
                    <view class="pop-item2-2">
                        <text>报修内容</text>
                        <text>维修进展</text>
                    </view>
                    <view class="pop-item2-3">
                        <text>栓内配件缺失,缺失设备;直流水枪。</text>
                        <text>已补全</text>
                    </view>
                    <view class="pop-item2-4">
                        <text>消防栓玻璃有破损</text>
                        <text>未修复</text>
                    </view>
                    <view class="pop-item2-5">
                        <text>干粉灭火器标示缺失</text>
                        <text>已修复</text>
                    </view>
                    <view class="pop-item2-6">
                        <text>干粉灭火器筒体受损</text>
                        <text>已修复</text>
                    </view>
                </view>
            </view>
            <view class="input0">
                <button type="default">取消</button>
                <button type="warn">确定</button>
            </view>
        </uni-popup>
        
        
        <!-- 顶部弹出层结束 -->
        <!-- 底部 弹出层开始-->
        <!-- <uni-popup :show="showPopupBottom" :type="popType" :msg="msg" v-on:hidePopup="hidePopup"> -->
        <uni-popup :show="dateCheck" position="bottom" @hidePopup="open">
            <view v-if="timeData.lunar" class="calendar-info">
                <view v-if="tags[0].checked">农历日期 : {{ timeData.year + '年' + timeData.month + '月' + timeData.date + '日 (' + timeData.lunar.astro + ')' }}</view>
                <view v-if="tags[0].checked">
                    {{ timeData.lunar.gzYear + '年' + timeData.lunar.gzMonth + '月' + timeData.lunar.gzDay + '日 (' + timeData.lunar.Animal + '年)' }}
                    {{ timeData.lunar.IMonthCn + timeData.lunar.IDayCn }} {{ timeData.lunar.isTerm ? timeData.lunar.Term : '' }}
                </view>
            </view>
            <view v-if="show" class="calendar-mask" @click="open">
                <view class="calendar-box" @click.stop="">
                    <uni-calendar :lunar="tags[0].checked" :fixed-heihgt="tags[1].checked" :slide="slide" :disable-before="tags[6].checked" :start-date="startDate" :end-date="endDate" :date="date" @change="change" @to-click="toClick" />
                    <view class="calendar-button-groups">
                        <button class="calendar-button-confirm" @click="confirm">确认</button>
                        <button class="calendar-button-confirm" @click="open">取消</button>
                    </view>
                </view>
            </view>
        </uni-popup>
      <!-- </uni-popup> -->
       <!-- 底部弹出层结束 -->
    </view>
</template>

<script>
    import uniSegmentedControl from '../../components/uni-segmented-control.vue';
    import uniPopup from '../../components/uni-popup/uni-popup.vue';
    import mediaList from '@/components/mediaList.vue';
    import uniLoadMore from '@/components/uni-load-more.vue';
    import uniIcon from '../../components/uni-icon.vue';
    import uniCalendar from '@/components/uni-calendar/uni-calendar.vue';

    export default {
         components: {
            uniSegmentedControl,
            uniPopup,
            mediaList,
            uniLoadMore,
            uniIcon,
            uniCalendar
        },
        data() {
            /**
             * 时间计算
             */
            function getDate(date, AddDayCount = 0) {
                if (typeof date !== 'object') {
                    date = date.replace(/-/g, '/')
                }
                let dd = new Date(date)
                dd.setMonth(dd.getMonth() + AddDayCount) // 获取AddDayCount天后的日期
                let y = dd.getFullYear()
                let m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
                let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
                return y + '-' + m + '-' + d
            }
            let tags = [{
                    id: 0,
                    name: '农历',
                    checked: false,
                    attr: 'lunar'
                },
                {
                    id: 1,
                    name: '固定高度',
                    checked: false,
                    attr: 'fixedHeihgt'
                },
                {
                    id: 2,
                    name: '垂直滚动',
                    checked: false,
                    attr: 'vertical'
                },
                {
                    id: 3,
                    name: '水平滚动',
                    checked: false,
                    attr: 'horizontal'
                },
                {
                    id: 4,
                    name: '开始日期(' + getDate(new Date(), -1) + ')',
                    checked: false,
                    value: getDate(new Date(), -1),
                    attr: 'startDate'
                },
                {
                    id: 5,
                    name: '结束日期(' + getDate(new Date(), 2) + ')',
                    value: getDate(new Date(), 2),
                    checked: false,
                    attr: 'endDate'
                },
                {
                    id: 6,
                    name: '禁用今天之前的日期',
                    checked: false,
                    attr: 'disableBefore'
                },
                {
                    id: 7,
                    name: '自定义当前日期(' + getDate(new Date(), 1) + ')',
                    value: getDate(new Date(), 1),
                    checked: false,
                    attr: 'date'
                }
            ]
            return {
                show: false,
                tags,
                slide: 'none',
                date: '',
                startDate: '',
                endDate: '',
                searchDate:'',
                dateCheck1:false,
                dateCheck:false,
                timeData: {},
                items: [
                    '报修中',
                    '已处理',
                    
                ],
                searchTypes:[
                    '报修时间',
                    '维修状态'
                ],
                typeIndex: false,               
                styles: [{
                    value: 'button',
                    text: '按钮'
                }, {
                    value: 'text',
                    text: '文字'
                }],
                colors: [
                    '#007aff',
                    '#4cd964',
                    '#dd524d'
                ],
                current: 0,
                activeColor: 'red',
                styleType: 'text',
                styleIndex: 0,
                colorIndex: 0,
                showImg: false,
                list: [{
                        title: "报修编号001",
                        img: "/static/images/2级/2设备报修/报修编号-icon.png"
                    },
                    {
                        title: "巡检编号002",
                        img: "/static/images/2级/2设备报修/编号.png"
                    },
                    {
                        title: "2019年3月29日",
                        img: "/static/images/2级/2设备报修/时间.png"
                    },
                    {
                        title: "松江区莘砖公路518号34号楼5楼",
                        img: "/static/images/2级/2设备报修/位置.png"
                    }

                ],
                popType: 'middle',
                title: 'popup',
                showPopupMiddle: false,
                showPopupTop: false,
                showPopupBottom: false,
                msg: ''

            }
        },
        onLoad() {
            setTimeout(() => {
                this.showImg = true;
            }, 400);
            
        },
       
        methods: {
            onClickItem(index) {
                this.typeIndex = !this.typeIndex;
            },
            onClickItemTop(index) {
                if (this.current !== index) {
                    this.current = index;
                }
            },
            styleChange(evt) {
                const value = evt.target.value;
                if (this.styleType !== value) {
                    this.styleType = value;
                    for (let i = 0; i < this.styles.length; i++) {
                        if (this.styles[i].value === value) {
                            this.styleIndex = i;
                            break;
                        }
                    }
                }
            },
            colorChange(evt) {
                if (this.styleType !== evt.target.value) {
                    this.activeColor = evt.target.value;
                    this.colorIndex = this.colors.indexOf(evt.target.value);
                }
            },
            //统一的关闭popup方法
            hidePopup: function() {
                this.showPopupMiddle = false;
                this.showPopupTop = false;
                this.showPopupBottom = false;
            },
            //展示居中 popup
            showMiddlePopup: function() {
                this.hidePopup();
                this.popType = 'middle';
                this.showPopupMiddle = true;
            },
            //展示顶部 popup
            showTopPopup: function() {
                this.hidePopup();
                this.popType = 'top';
                this.msg = '顶部 popup 信息内容';
                this.showPopupTop = true;
            },
            //展示底部 popup
            showBottomPopup: function() {
                this.hidePopup();
                this.popType = 'bottom';
                this.msg = '底部 popup 信息内容';
                this.showPopupBottom = true;
            },
            closeMask() {
                            this.show = false
                        },
                        toggle(index, item) {
                            this.tags[index].checked = !item.checked
                            // item.checked = !item.checked;
                            console.log(index)
                            if (index === 2) {
                                this.tags[3].checked = false
                            }
                            if (index === 3) {
                                this.tags[2].checked = false
                            }
                            // this.attribute[item.attr] = !item.checked;
                        },
                        open() {
                            this.dateCheck = !this.dateCheck;
                            console.log(this.dateCheck)
                            if(this.dateCheck){
                                if (this.tags[3].checked) {
                                    this.slide = 'horizontal'
                                } else if (this.tags[2].checked) {
                                    this.slide = 'vertical'
                                } else {
                                    this.slide = 'none'
                                }
                                if (this.tags[4].checked) {
                                    this.startDate = this.tags[4].value
                                } else {
                                    this.startDate = ''
                                }
                                if (this.tags[5].checked) {
                                    this.endDate = this.tags[5].value
                                } else {
                                    this.endDate = ''
                                }
                                if (this.tags[7].checked) {
                                    this.date = this.tags[7].value
                                } else {
                                    this.date = ''
                                }
                                this.show = true
                            }
                            
                        },
                        change(e) {
                            console.log('change 返回:', e.fulldate)
                            this.timeData = e
                        },
                        toClick(e) {
                            console.log('点击事件', e.fulldate)
                            this.timeData = e
                        },
                        confirm() {
                            this.show = false;
                            this.dateCheck = !this.dateCheck;
                            this.searchDate=this.timeData.fulldate;
                            console.log(this.searchDate)
                        }
                }
           }
</script>
      

<style>
   
    .t1 {
        color:#FFFFFF;
    }
    .weixiu {
        position: relative;
    }
    .t {
        position: absolute;
        left: 38upx;
        top: 20upx;
        font-size: 30upx;
    }
    .wei {
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .weixiu image {
        width: 168upx;
        height: 100upx;
    }
    
    .top {
        width: 360upx;
        height: 60upx;
        border-radius: 50upx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 40upx;
    }
    .input-text {
        width: 80upx;
        height: 26upx;
        font-family: PingFang-SC-Regular;
        font-size: 28upx;
        color: #666666;
        padding-left: 30upx;
        
    }

    .input-item {
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
    }

    .input-item0 {
        width: 520upx;
        height: 80upx;
        background-color: #ffffff;
        box-shadow: 0px 10upx 19upx 1upx rgba(0, 0, 0, 0.1);
        border-radius: 40upx;
        padding-left: 80upx;
    }

    .input-img1 {
        width: 24upx;
        height: 24upx;
        position: absolute;
        left: 60upx;
        top: 28upx;
    }

    .input-img2 {
        width: 28upx;
        height: 28upx;
        position: absolute;
        right: 210upx;
        top: 28upx;
    }

    .input-img3 {
        width: 28upx;
        height: 28upx;
        position: absolute;
        right: 160upx;
        top: 28upx;
    }

    page {
        height: auto;
        background-color: #f1f1f1;
    }

    .container {
        position: relative;
    }

    .container-title {
        position: absolute;
        left: 0upx;
        top: -50upx;
        width: 309upx;
        height: 49upx;
        display: flex;
        justify-content: space-between;

    }

    .header {
        width: 500upx;
        font-size: 38upx;
        color: #333333;
    }

    .content {
        margin: 60upx auto;
        padding-left: 30upx;
    }

    .color-tag {
        width: 50upx;
        height: 50upx;
    }

    /* 保修时间内容开始*/

    .red-item {
        position: absolute;
        right: 40upx;
        top: 0;
        display: flex;
        flex-direction: column;

    }

    .red {
        width: 60upx;
        height: 140upx;
    }

    .baoxiu {
        font-size: 24upx;
        color: #FFFFFF;
        width: 22upx;
        height: 73upx;
        position: absolute;
        left: 18upx;
        top: 18upx;
    }

    .uni-list {
        width: 690upx;
        height: 300upx;
        background-color: #ffffff;
        box-shadow: 0px 23upx 27upx 2upx rgba(0, 0, 0, 0.11);
        border-radius: 20upx;
        box-sizing: border-box;
        padding: 30upx;
        margin-top: 50upx;
        position: relative;
    }

    .list-img {
        width: 28upx;
        height: 28upx;
    }

    .uni-media-list {
        display: flex;
        justify-content: flex-start;
        align-items: center;

    }

    .uni-media-list-text-top {
        font-size: 28upx;
        color: #666;
        padding: 10upx;
    }

    .current1,
    .current2 {
        display: flex;
        flex-direction: column;
    }

    /* 保修时间内容结束 */
    /* 顶部弹出层 开始*/
    .pop {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        position: relative;
    }

    .pop-title {
        width: 680upx;
        height: 102upx;
        background-color: #C30000;
        color: #FFFFFF;
        text-align: center;
        line-height: 102upx;
        font-size: 36upx;
        font-family: PingFang-SC-Bold;
        position: absolute;
        left: -15upx;
        top: -180upx;
    }
    .pop-item1 {
        box-sizing:border-box;
        padding-bottom: 20upx;
    }
    .pop-item1 view {
        font-family: PingFang-SC-Regular;
        font-size: 28upx;
        font-weight: normal;
        color: #666666;
        margin: 20upx;
    }

    .pop-item2 {
        display: flex;
        flex-direction: column;
    }

    .pop-item2-1 {
        display: flex;
        justify-content: space-between;
    }

    .pop-item2-1 text {
        font-size: 24upx;
        color: #666666;
    }

    .pop-item2-2 {
        display: flex;
        justify-content: space-between;
        width: 650upx;
        height: 70upx;
        background-color: #dddddd;
        align-items: center;
        font-size: 28upx;
        color: #333333;
        box-sizing: border-box;
        padding: 20upx;
    }

    .pop-item2-2 text {
        font-family: PingFang-SC-Medium;
        font-size: 30upx;
        color: #333333;

    }

    .pop-item2-3 {
        display: flex;
        justify-content: space-between;
        margin: 20upx;
    }

    .pop-item2-4 {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 650upx;
        height: 50upx;
        background-color: #f5f5f5;
        box-sizing: border-box;
        padding: 20upx;
    }

    .pop-item2-5 {
        display: flex;
        justify-content: space-between;
        margin: 20upx;
    }

    .pop-item2-6 {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 650upx;
        height: 50upx;
        background-color: #f5f5f5;
         box-sizing: border-box;
        padding: 20upx;
    }

    .pop-item2-3 text {
        font-size: 24upx;
        color: #666666;
    }

    .pop-item2-4 text {
        font-size: 24upx;
        color: #666666;
    }

    .pop-item2-5 text {
        font-size: 24upx;
        color: #666666;
    }

    .pop-item2-6 text {
        font-size: 24upx;
        color: #666666;
    }
    .input0 {
        display: flex;
       justify-content: center;
       align-items: center;
       position: absolute;
       left: 0;
       bottom: 0;    
    }
    .input0 button {
        width: 340upx;
        height: 102upx;
    }

    /* 顶部弹出层 结束*/
</style>
 

好的,可以使用JavaScript和HTML/CSS来实现一个简单的日历,并且在点击日期弹出一个对话框,让用户设置价格和房间数量。以下是一个示例代码: HTML代码: ``` <div id="calendar"></div> <div id="dialog" style="display:none;"> <h3 id="date"></h3> <form> <label>价格:</label> <input type="number" id="price" required><br> <label>房间数量:</label> <input type="number" id="rooms" required><br> <button type="submit" id="submit-btn">提交</button> </form> </div> ``` CSS代码: ``` #calendar { border: 1px solid #ccc; padding: 10px; width: 300px; margin: 0 auto; } #calendar table { width: 100%; } #calendar th, #calendar td { text-align: center; padding: 5px; } #calendar th { background-color: #eee; } #calendar td:hover { background-color: #f5f5f5; cursor: pointer; } #dialog { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: #fff; padding: 20px; border: 1px solid #ccc; box-shadow: 0 0 10px rgba(0,0,0,0.3); } #dialog label { display: block; margin-bottom: 5px; } #dialog input { width: 100%; padding: 5px; margin-bottom: 10px; } #dialog button { display: block; margin: 10px auto; padding: 5px 10px; background-color: #007bff; color: #fff; border: none; cursor: pointer; } ``` JavaScript代码: ``` // 获取日历容器元素 const calendarEl = document.getElementById('calendar'); // 获取对话框元素和日期元素 const dialogEl = document.getElementById('dialog'); const dateEl = document.getElementById('date'); // 获取价格和房间数量输入框及提交按钮元素 const priceEl = document.getElementById('price'); const roomsEl = document.getElementById('rooms'); const submitBtn = document.getElementById('submit-btn'); // 定义当前日期和月份 let currentDate = new Date(); let currentMonth = currentDate.getMonth(); let currentYear = currentDate.getFullYear(); // 定义当前选中日期和价格、房间数量 let selectedDate; let selectedPrice; let selectedRooms; // 定义月份名称和每个月的天数 const months = [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ]; const monthDays = [ 31, // 1月 28, // 2月 31, // 3月 30, // 4月 31, // 5月 30, // 6月 31, // 7月 31, // 8月 30, // 9月 31, // 10月 30, // 11月 31 // 12月 ]; // 判断是否为闰年 function isLeapYear(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } // 获取某个月的天数 function getMonthDays(year, month) { if (month === 1 && isLeapYear(year)) { return 29; } else { return monthDays[month]; } } // 获取某个月的第一天是星期几 function getFirstDayOfMonth(year, month) { return new Date(year, month, 1).getDay(); } // 获取某个日期的字符串表示 function formatDate(date) { return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; } // 渲染日历 function renderCalendar() { // 创建表格元素和表头元素 const tableEl = document.createElement('table'); const theadEl = document.createElement('thead'); // 创建表头的年份和月份 const yearMonthEl = document.createElement('th'); yearMonthEl.colSpan = 7; yearMonthEl.innerHTML = `${months[currentMonth]} ${currentYear}`; // 创建表头的星期几 const weekdaysEl = document.createElement('tr'); const weekdays = ['日', '一', '二', '三', '四', '五', '六']; weekdays.forEach((weekday) => { const thEl = document.createElement('th'); thEl.innerHTML = weekday; weekdaysEl.appendChild(thEl); }); // 将表头元素添加到表格中 theadEl.appendChild(yearMonthEl); theadEl.appendChild(weekdaysEl); tableEl.appendChild(theadEl); // 计算当前月份的天数和第一天是星期几 const monthDays = getMonthDays(currentYear, currentMonth); const firstDay = getFirstDayOfMonth(currentYear, currentMonth); // 创建表格的日期部分 let date = 1; for (let i = 0; i < 6; i++) { const trEl = document.createElement('tr'); for (let j = 0; j < 7; j++) { const tdEl = document.createElement('td'); if (i === 0 && j < firstDay) { // 不属于本月的日期 tdEl.innerHTML = ''; tdEl.classList.add('disabled'); } else if (date > monthDays) { // 超过本月的日期 tdEl.innerHTML = ''; tdEl.classList.add('disabled'); } else { // 属于本月的日期 tdEl.innerHTML = date; tdEl.classList.add('date'); if (date === currentDate.getDate() && currentMonth === currentDate.getMonth() && currentYear === currentDate.getFullYear()) { tdEl.classList.add('today'); } date++; } trEl.appendChild(tdEl); } tableEl.appendChild(trEl); } // 将表格元素添加到日历容器中 calendarEl.innerHTML = ''; calendarEl.appendChild(tableEl); // 绑定日期单元格的点击事件 const dateEls = document.querySelectorAll('.date'); dateEls.forEach((dateEl) => { dateEl.addEventListener('click', () => { selectedDate = new Date(currentYear, currentMonth, dateEl.innerHTML); dateEl.classList.add('selected'); dialogEl.style.display = 'block'; dateEl.innerHTML += `<br><small>¥${selectedPrice} / ${selectedRooms}间</small>`; dateEl.style.position = 'relative'; dateEl.style.zIndex = 1; dateEl.style.backgroundColor = '#007bff'; dateEl.style.color = '#fff'; dateEl.style.borderRadius = '50%'; dateEl.style.padding = '10px'; dateEl.style.boxSizing = 'border-box'; dateEl.style.boxShadow = '0 0 10px rgba(0,0,0,0.3)'; priceEl.focus(); }); }); // 绑定对话框的提交事件 submitBtn.addEventListener('click', (event) => { event.preventDefault(); selectedPrice = priceEl.value; selectedRooms = roomsEl.value; dateEl.innerHTML = `${selectedDate.getMonth() + 1}月${selectedDate.getDate()}日`; dialogEl.style.display = 'none'; }); } // 渲染初始日历 renderCalendar(); // 绑定前一月和后一月按钮的点击事件 document.getElementById('prev-btn').addEventListener('click', () => { currentMonth--; if (currentMonth < 0) { currentMonth = 11; currentYear--; } renderCalendar(); }); document.getElementById('next-btn').addEventListener('click', () => { currentMonth++; if (currentMonth > 11) { currentMonth = 0; currentYear++; } renderCalendar(); }); ``` 以上代码实现了一个简单的日历,并且在点击日期弹出对话框,让用户设置价格和房间数量。在设置完价格和房间数量后,对话框会自动关闭,并且在选中日期下方显示设置的价格和房间数量。用户也可以通过左右箭头切换月份。你可以根据自己的需求进一步优化代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值