Front-End/J-Query

jQuery Tutorial

JQuery - MOME


JQuery 는 JavaScritp 라이브 러리


JQuery - 적게 쓰고 자바스크립트 효과를 낸다


"Try it Yourself" Examples in Each Chapter - 제이쿼리 실행


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p").click(function(){
        $(this).hide();
    });
});
</script>
</head>
<body>

<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

</body>
</html>

jQuery Introduction

  • HTML
  • CSS
  • JavaScript

What is jQuery? - jQuery 기능

  • HTML/DOM manipulation
  • CSS manipulation
  • HTML event methods
  • Effects and animations
  • AJAX
  • Utilities

Why jQuery?

적게쓰고 자바스크립트 효과를 낸다.
심플하고 강력하다.
많은 회사들이 jQuery 지원한다.

jQuery Get Started - 실행방법

Adding jQuery to Your Web Pages

  • Download the jQuery library from jQuery.com
  • Include jQuery from a CDN, like Google
* CDN 컨텐츠 딜리버리 네트워크 가장 가까운 네트워크에 연결해서 속도를 높인다.

Downloading jQuery - 다운로드 방법

사용방법

html 문서 내 <head>태그 안에 j-query src= 경로 작성

<head>

<script src="jquery-3.2.1.min.js">


</script>

</head>


기존

Do you wonder why we do not have type="text/javascript" inside the <script> tag?

text 사라진 이유??

html5 는 기본 디폴트 랭기지가 자바스크립트라서 text/javascript 없어도됨

참고 바람~

사용법 html <head> 태그에 주소 한줄 넣어주면 됨

jQuery CDN

구글

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">


</script>

</head>


MS

<head>
   <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js">


</script>

</head>

jQuery Syntax - 문법

jQuery Syntax

Basic syntax is: $(selector).action()

  • A $ sign to define/access jQuery
  • A (selector) to "query (or find)" HTML elements
  • A jQuery action() to be performed on the element(s)

Examples:

$(selector).action() // 준비

// 액션

$(this).hide() - hides the current element.        // html 엘리먼트 현재 선택자

$("p").hide() - hides all <p> elements.            // html p 태그 선택

$(".test").hide() - hides all elements with class="test". // 클래스

$("#test").hide() - hides the element with id="test".   // 아이디 지정


제이쿼리 준비

$(document).ready(function(){

   // jQuery methods go here...

});


The Document Ready Event - 이벤트 준비

$(document).ready(function(){        // 준비가 되어야 실행이되고 액션을 실행

   // jQuery methods go here...

});

jQuery Selectors

The element Selector - html 선택자 선택 방법

$("p")    // html 태그 제어


<!DOCTYPE html>   // doctype을 html 지정

<html>            

<head>            // 헤드 태그 사이에 cdn 로딩

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>

$(document).ready(function(){        // document 가 로딩되었는지 확인

    $("button").click(function(){    // 버튼을 클릭했을때 펑션 작동

        $("p").hide();               // btn 클릭했을때 p 태그가 히든 됨

    });

});

</script>

</head>

<body>


<h2>This is a heading</h2>


<p>This is a paragraph.</p>

<p>This is another paragraph.</p>


<button>Click me to hide paragraphs</button>


</body>

</html>

The #id Selector - html id 선택자

$("#test")
<!DOCTYPE html>

<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>

$(document).ready(function(){

    $("button").click(function(){

        $("#test").hide();

    });

});

</script>

</head>

<body>


<h2>This is a heading</h2>


<p>This is a paragraph.</p>

<p id="test">This is another paragraph.</p>


<button>Click me</button>


</body>

</html>

The .class Selector - html class 선택자

$(".test")

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $(".test").hide();        // html 내 클라스 선택
    });
});
</script>
</head>
<body>

<h2 class="test">This is a heading</h2>
<p class="test">This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Click me</button>

</body>
</html>

그외

More Examples of jQuery Selectors 방법

SyntaxDescriptionExample
$("*")Selects all elementsTry it
$(this)Selects the current HTML elementTry it
$("p.intro")Selects all <p> elements with class="intro"Try it
$("p:first")Selects the first <p> elementTry it
$("ul li:first")Selects the first <li> element of the first <ul>Try it
$("ul li:first-child")Selects the first <li> element of every <ul>Try it
$("[href]")Selects all elements with an href attributeTry it
$("a[target='_blank']")Selects all <a> elements with a target attribute value equal to "_blank"Try it
$("a[target!='_blank']")Selects all <a> elements with a target attribute value NOT equal to "_blank"Try it
$(":button")Selects all <button> elements and <input> elements of type="button"Try it
$("tr:even")Selects all even <tr> elementsTry it
$("tr:odd")Selects all odd <tr> elements
해설 
$("*") 전부 모두 선택한다.

$(this) 현재 그것을 선택한 그 자체 즉 선택한다.

$("p.intro") P태그 안에 클래스 이름이 intro 선택한다.


$("p:first") 처음 P태그만 선택한다.(여러가지 P태그중에 첫번째꺼만 선택)


$("ul li:first") ul에 li 중에 첫번째 한개만 선택한다.(ul 태그 안에 li태그중에 첫번째꺼만 선택) - 한개만


$("ul li:first-child") ul에서 첫번째 모두 li개수만큼 선택한다.(ul태그 안에 li태그 모두 선택) - li 모두


$("[href]") href 하이퍼 레퍼런스 모두 선택 href="http://www.w3school.com"


$("a[target='blank']") <p><a href="https://www.w3schools.com/html/" target="_blank">HTML Tutorial</a></p> 만 선택한다.


$("a[target!='blank']") != 부정연산자 target='blank 아닌것만 선택한다.


$(":button") 모든 버튼을 선택한다.


$("tr:even") tr태그 even 짝수 즉! 테이블 태그에서 짝수줄만 선택한다. 


$("tr:odd") tr태그에서 odd 홀수 즉! 테이블 태그에서 홀수줄만 선택한다.


Functions In a Separate File 외부파일 불러오기

<head>
   <scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
   

</script>
<script src="my_jquery_functions.js">


</script>

</head>


제이쿼리 핵심 이벤트

j-Query Event Methods

Mouse EventsKeyboard EventsForm EventsDocument/Window Events
clickkeypresssubmitload
dblclickkeydownchangeresize
mouseenterkeyupfocusscroll
mouseleave blurunload

jQuery Syntax For Event Methods

$("p").click();    // p 선택자 .click(); 이벤트 = 선택자 + 이벤트      

$("p").click(function(){
  // action goes here!!
});

Commonly Used jQuery Event Methods

$(document).ready()    // 제인쿼리 로딩 및 준비

click() 이벤트 사용법

$("p").click(function(){   // p태그 선택자 이벤트 클릭하면
    $(this).hide();        // this = all 지금 hide 숨김
});


dblclick() 더블클릭 이벤트 사용법 

$("p").dblclick(function(){ // dblclick 이벤트 메소드
    $(this).hide();         // 더블클릭하는 선택자(객체) 그것을 this
});

mouseenter() 마우스 엔터 사용법
$("#p1").mouseenter(function(){  // html id가 p1 선택하고
    alert("You entered p1!");    // mouseenter 마우스를 가져가면 alert("msg출력");
});
<p id="p1">Enter this paragraph.</p>

mouseleave() 마우스 떠나면 - 리브
$("#p1").mouseleave(function(){    // html id가 p1 선택하고
    alert("Bye! You now leave p1!");// mouseleave 마우스를 때면 alert("msg출력");
});
<p id="p1">This is a paragraph.</p>

mousedown() - 마우스버튼을 클릭하면은
$("#p1").mousedown(function(){
    alert("Mouse down over p1!"); // 마우스를 다운하면 alert 안에 msg 출력 ~
});
<p id="p1">This is a paragraph.</p>

mouseup() - 마우스 버튼 업 하면
$("#p1").mouseup(function(){
    alert("Mouse up over p1!");    // 마우스를 업하면 alert 안에 msg 출력~
});
<p id="p1">This is a paragraph.</p>


The on() Method

EX)1
<script>
$(document).ready(function(){
    $("p").on("click", function(){
        $(this).hide();
    });
});
</script>
</head>
<body>

<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

</body>
</html>
EX)2
<script>
$(document).ready(function(){
    $("p").on({
        mouseenter: function(){
            $(this).css("background-color", "lightgray");
        },  
        mouseleave: function(){
            $(this).css("background-color", "lightblue");
        }, 
        click: function(){
            $(this).css("background-color", "yellow");
        }  
    });
});
</script>
</head>
<body>

<p>Click or move the mouse pointer over this paragraph.</p>

</body>
</html>

참고

https://www.w3schools.com/jquery/jquery_events.asp


제인쿼리 이벤트 레퍼런스 그때 그때 참고해서 사용하면됨

https://www.w3schools.com/jquery/jquery_ref_events.asp


'Front-End > J-Query' 카테고리의 다른 글

jQuery Misc  (0) 2017.07.06
jQuery AJAX  (0) 2017.07.06
jQuery Traversing  (0) 2017.07.06
jquery HTML  (0) 2017.07.06
jQuery Effects  (0) 2017.07.06
,

최근 댓글

최근 트랙백

알림

이 블로그는 구글에서 제공한 크롬에 최적화 되어있고, 네이버에서 제공한 나눔글꼴이 적용되어 있습니다.

태그

카운터

Today :
Yesterday :
Total :