본문 바로가기
카테고리 없음

jquery로 날자 구하기

by soLow 2016. 3. 21.
SMALL

 

        <script src= "http://code.jquery.com/jquery-1.9.1.min.js" ></script>

           <script type="text/javascript">
            $(document).ready( function() {
                var now = new Date();
                var month = (now.getMonth() + 1);              
                var day = now.getDate();
                var yesterday = (now.getDate() - 3);


                if(month < 10)
                    month = "0" + month;
                if(day < 10)
                    day = "0" + day;
                var today = now.getFullYear() + '-' + month + '-' + day;
                var fromdate = now.getFullYear() + '-' + month + '-' + "01";
                var todate = now.getFullYear() + '-' + month + '-' + day;
               
                $('#date1').val(fromdate);
                $('#date2').val(todate);
                $('#bt_search').trigger('click'); //조회하자마자 버튼클릭
               
            });

 

조회하면을 만들다보면 초기값으로 1일부터 오늘까지 또는 데이터양이나 화면의 용도에따라..

몇일 전부터 오늘까지 초기값으로 넣는 경우가 많다..

언어마다 문법이 조금씩 다르지만…

웹에서 많아쓰는 java script 를 예를 들어보면…

 

위와같다…

일단 jquery 를 사용하기 위해

<script src= "http://code.jquery.com/jquery-1.9.1.min.js" ></script>

jquery 링크를 첨부해주었고

 

화면이 실행되자마자 실행하기 위하여

    $(document).ready( function() {

위 펑션에 코드를 삽입하였다.

 

 

일단 Date 객체에서 당일 객체를 가져온후

month, day, year 값을 가져온다.

 

객체에 문자열을 더할경우 자동으로 형변환이 이루어져 문자열 편집이되고

var 변수에 값을 할당하는 경우 처럼  숫자를 더하거나 뺄경우도 역시 숫자로 형변환이 된다.

 

마지막으로

jquery 의 객체 지정자를 통하여 date1, date2 에 생성된 날자 초기값을 할당하면된다.

 

참고로 jquery 에는 셀렉터라고하는  지정자가 있는데..

 

굳이 html 코드내에 일일히 지정하지 않아도

 

jQuery Selectors

$("p").hide()
Demonstrates the jQuery hide() method, hiding all <p> elements.

$("#test").hide()
Demonstrates the jQuery hide() method, hiding the element with id="test".

$(".test").hide()
Demonstrates the jQuery hide() method, hiding all elements with class="test".

$(this).hide()
Demonstrates the jQuery hide() method, hiding the current HTML element.

 

#은 아이디를 지정할때 사용되고

class 를 지정할땐 .을 사용한다.

this 는 현재 html 을 지정한다

 

참고로 많이 사용되는 지정자들을 모아보았다..

 

More Examples of jQuery Selectors

Syntax
Description
Example

$("*")
Selects all elements (모든 엘레먼트 지정) –> 거의 쓰일일이 없다

$(this)
Selects the current HTML element (현재 문서)

$("p.intro")
Selects all <p> elements with class="intro" (클래스가 intro 인 p 태그)

$("p:first")
Selects the first <p> element(첫번째 p 태그)

$("ul li:first")
Selects the first <li> element of the first <ul>  (첫번째 ul 태그)

$("ul li:first-child")
Selects the first <li> element of every <ul> (모든 ul 의 첫번째 li 태그)

$("[href]")
Selects all elements with an href attribute ( 태그의 특정 속성만 지정)

$("a[target='_blank']")
Selects all <a> elements with a target attribute value equal to "_blank"  ( 별도창을 의우는 a 태그 지정 )

$("a[target!='_blank']")
Selects all <a> elements with a target attribute value NOT equal to "_blank" ( 별도창을 띄우지 않는 a 태그)

$(":button")
Selects all <button> elements and <input> elements of type="button" ( 모든 버튼 지정)

$("tr:even")
Selects all even <tr> elements ( tr중 홀수)

$("tr:odd")
Selects all odd <tr> elements (tr 중 짝수)

LIST