메뉴 건너뛰기

조회 수 513 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

특정 영역을 지정하여 해당 영역에 달력을 생성하고 클릭에 따라 선택 이벤트, 이전달, 다음달로 이동되는 달력 code를 작성해보겠습니다.

아래는 동작 달력입니다.

 

 

 

 

 

 

 

2021년 3

  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

 

 

 

달력만들기

calendar.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>달력 만들기</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <div id="calendarForm"></div>
</body>
</html>

 

calendar.css

* {
    margin: 0;
    padding: 0
}

.custom_calendar_table td {
    text-align: center;
}

.custom_calendar_table thead.cal_date th {
    font-size: 1.5rem;
}

.custom_calendar_table thead.cal_date th button {
    font-size: 1.5rem;
    background: none;
    border: none;
}

.custom_calendar_table thead.cal_week th {
    background-color: #288CFF;
    color: #fff;
}

.custom_calendar_table tbody td {
    cursor: pointer;
}

.custom_calendar_table tbody td:nth-child(1) {
    color: red;
}

.custom_calendar_table tbody td:nth-child(7) {
    color: #288CFF;
}

.custom_calendar_table tbody td.select_day {
    background-color: #288CFF;
    color: #fff;
}

 

calendar.js

(function () {
    calendarMaker($("#calendarForm"), new Date());
})();

var nowDate = new Date();
function calendarMaker(target, date) {
    if (date == null || date == undefined) {
        date = new Date();
    }
    nowDate = date;
    if ($(target).length > 0) {
        var year = nowDate.getFullYear();
        var month = nowDate.getMonth() + 1;
        $(target).empty().append(assembly(year, month));
    } else {
        console.error("custom_calendar Target is empty!!!");
        return;
    }

    var thisMonth = new Date(nowDate.getFullYear(), nowDate.getMonth(), 1);
    var thisLastDay = new Date(nowDate.getFullYear(), nowDate.getMonth() + 1, 0);


    var tag = "<tr>";
    var cnt = 0;
    //빈 공백 만들어주기
    for (i = 0; i < thisMonth.getDay(); i++) {
        tag += "<td></td>";
        cnt++;
    }

    //날짜 채우기
    for (i = 1; i <= thisLastDay.getDate(); i++) {
        if (cnt % 7 == 0) { tag += "<tr>"; }

        tag += "<td>" + i + "</td>";
        cnt++;
        if (cnt % 7 == 0) {
            tag += "</tr>";
        }
    }
    $(target).find("#custom_set_date").append(tag);
    calMoveEvtFn();

    function assembly(year, month) {
        var calendar_html_code =
            "<table class='custom_calendar_table'>" +
            "<colgroup>" +
            "<col style='width:81px'/>" +
            "<col style='width:81px'/>" +
            "<col style='width:81px'/>" +
            "<col style='width:81px'/>" +
            "<col style='width:81px'/>" +
            "<col style='width:81px'/>" +
            "<col style='width:81px'/>" +
            "</colgroup>" +
            "<thead class='cal_date'>" +
            "<th><button type='button' class='prev'><</button></th>" +
            "<th colspan='5'><p><span>" + year + "</span>년 <span>" + month + "</span>월</p></th>" +
            "<th><button type='button' class='next'>></button></th>" +
            "</thead>" +
            "<thead  class='cal_week'>" +
            "<th>일</th><th>월</th><th>화</th><th>수</th><th>목</th><th>금</th><th>토</th>" +
            "</thead>" +
            "<tbody id='custom_set_date'>" +
            "</tbody>" +
            "</table>";
        return calendar_html_code;
    }

    function calMoveEvtFn() {
        //전달 클릭
        $(".custom_calendar_table").on("click", ".prev", function () {
            nowDate = new Date(nowDate.getFullYear(), nowDate.getMonth() - 1, nowDate.getDate());
            calendarMaker($(target), nowDate);
        });
        //다음날 클릭
        $(".custom_calendar_table").on("click", ".next", function () {
            nowDate = new Date(nowDate.getFullYear(), nowDate.getMonth() + 1, nowDate.getDate());
            calendarMaker($(target), nowDate);
        });
        //일자 선택 클릭
        $(".custom_calendar_table").on("click", "td", function () {
            $(".custom_calendar_table .select_day").removeClass("select_day");
            $(this).removeClass("select_day").addClass("select_day");
        });
    }
}

 


  1. DTREE 트리구조 만들기

  2. No Image 09Mar
    by
    2021/03/09 Views 730 

    Javascript - 유효성 체크(이메일 정규식, IP 정규식, 비밀번호 등)

  3. HTML, Javscript - 선택한 색상으로 배경색 바꾸기(pallet 만들기)

  4. No Image 09Mar
    by 조쉬
    2021/03/09 Views 513 

    Javascript - Calendar 달력 생성하고 제어하기

  5. javascript - vanillaJS로 체크박스(checkbox) 제어하기

  6. No Image 25Mar
    by
    2021/03/25 Views 321 

    자바스크립트에서 이벤트 중단 하는 방법

  7. No Image 09Mar
    by
    2021/03/09 Views 302 

    Javascript - 입력받은 숫자를 순서대로 홀짝 별로 배열에 삽입하기

  8. No Image 09Mar
    by
    2021/03/09 Views 268 

    Javascript - form태그 내부 ajax처리시 2번 전송되는 현상

  9. No Image 20Aug
    by
    2021/08/20 Views 261 

    대소문자 변경 (대문자를 소문자로, 소문자를 대문자로)

  10. No Image 20Aug
    by
    2021/08/20 Views 242 

    월의 마지막 날짜 계산하기

  11. No Image 25Mar
    by
    2021/03/25 Views 237 

    오브젝트 속성

  12. No Image 26Mar
    by
    2021/03/26 Views 225 

    3자리 마다 쉼표만 찍어주는 number_format 함수

  13. No Image 09Mar
    by
    2021/03/09 Views 224 

    Javascript - 입력한 년, 월의 마지막 날짜 구하기

  14. No Image 20Aug
    by
    2021/08/20 Views 223 

    Date 객체로 원하는 날짜, 시간 표현하기

  15. No Image 25Mar
    by
    2021/03/25 Views 218 

    현재 날짜, 시간 ( Month + 1 에 대해서 )

  16. No Image 26Mar
    by
    2021/03/26 Views 215 

    폼안에 태그명, 함수명 같을때 오류

  17. No Image 26Mar
    by
    2021/03/26 Views 204 

    split, join, replace, replace_all

  18. No Image 26Mar
    by
    2021/03/26 Views 203 

    자주쓰는 것들

  19. No Image 26Mar
    by
    2021/03/26 Views 199 

    default 매개변수(매개변수 기본값)

  20. No Image 26Mar
    by
    2021/03/26 Views 197 

    getYear(); 크롬, 파이어폭스 에서 제대로 작동 안하는 문제

Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved