<head>
에 <Script>
태그를 넣게 된다면 $(function(){});
이 안에 jQuery를 넣어야됨.$()
.$("p")
$("#test")
$(".test")
mouseenter()
: 마우스 포인터가 HTML 요소에 들어갈 때 함수가 실행됨.mouseleave()
: 마우스 포인터가 HTML 요소를 떠날때 함수가 실행됨mousedown()
: 왼쪽, 가운데 또는 오른쪽 버튼을 누르면 마우스가 html 요소 위에 있는 동안 실행됨.mouseup()
: 마우스가 HTML 요소 위에 있는 동안 왼쪽, 가운데 또는 오른쪽 버튼이 놓일 때 기능이 실행됨.hover()
: mouseenter()
+ mouseleave()
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
});
//첫 번째 함수는 마우스가 HTML 요소에 들어갈 때 실행되고, 두 번째 함수는 마우스가 HTML 요소를 떠날 때 실행됩니다.
focus()
: input 필드가 포커스를 받을 때(그러니까 거기로 커서가 올때) 사용됨blur()
: input 필드가 포커스를 잃을 때 실행됨.on()
: 메서드는 선택된 요소에 하나 이상의 이벤트 핸들러를 연결함//click 이벤트를 on으로 넣고싶을때
$("p").on("click", function(){
$(this).hide();
});
//3개 연결
$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
}
});
-> 대부분의 Effects에서는 끝나고 callback 함수를 바로 호출하여 끝나자마자 올 동작에 대해서 지정할 수 있음.
$(selector).hide(speed,callback)
, $(selector).show(speed,callback);
$("button").click(function(){
$("p").hide(1000);
});
$(selector).toggle(speed,callback);
$(selector).fadeIn(speed,callback);
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
$(selector).fadeOut(speed,callback);
$(selector).fadeToggle(speed,callback);
$(selector).fadeTo(speed,opacity,callback);
$(selector).slideDown(speed,callback);
$("#flip").click(function(){
$("#panel").slideDown();
});
$(selector).slideUp(speed,callback);
$(selector).slideToggle(speed,callback);
slideDown()
+ slideUp()
animate()
method is used to create custom animations.$(selector).animate({params},speed,callback);
$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});
$("button").click(function(){
$("div").animate({
left: '250px',
height: '+=150px',
width: '+=150px'
});
});
$("button").click(function(){
$("div").animate({
height: 'toggle'
});
});
$("button").click(function(){
var div = $("div");
div.animate({height: '300px', opacity: '0.4'}, "slow");
div.animate({width: '300px', opacity: '0.8'}, "slow");
div.animate({height: '100px', opacity: '0.4'}, "slow");
div.animate({width: '100px', opacity: '0.8'}, "slow");
});
stop()
method is used to stop an animation or effect before it is finished.$(selector).stop(stopAll,goToEnd);
$("#stop").click(function(){
$("#panel").stop();
});
//callback함수가 있을때
$("button").click(function(){
$("p").hide("slow", function(){
alert("The paragraph is now hidden");
});
});
//callback 함수가 없을때
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
--> id가 p1인 요소에 대해 다음 작업을 순차적으로 수행:
1. css("color", "red"): 텍스트 색상을 빨간색으로 변경
2. slideUp(2000): 요소를 2초 동안 슬라이드 업하여 숨김
3. slideDown(2000): 요소를 2초 동안 슬라이드 다운하여 다시 표시함.
text()
- 선택한 요소의 텍스트 내용을 설정하거나 반환합니다.html()
- 선택한 요소의 내용을 설정하거나 반환합니다 (HTML 마크업 포함)val()
- 양식 필드의 값을 설정하거나 반환합니다.$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
$("#btn3").click(function(){
alert("Value: " + $("#test").val());
});
$("button").click(function(){
alert($("#w3s").attr("href"));
});
text()
- 선택한 요소의 텍스트 내용을 설정하거나 반환합니다.html()
- 선택한 요소의 내용을 설정하거나 반환합니다 (HTML 마크업 포함)val()
- 양식 필드의 값을 설정하거나 반환합니다.$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
$("button").click(function(){
$("#w3s").attr({
"href" : "https://www.w3schools.com/jquery/",
"title" : "W3Schools jQuery Tutorial"
});
});
//Callback 함수
//버튼을 클릭할 때마다 id="w3s"를 가진 요소(주로 링크)의 href 속성 값이 변경됩니다. 구체적으로, 기존 href 값에 "/jquery/"가 추가됩니다.
$("button").click(function(){
$("#w3s").attr("href", function(i, origValue){
return origValue + "/jquery/";
});
});
function appendText() {
var txt1 = "<p>Text.</p>"; // Create element with HTML
var txt2 = $("<p></p>").text("Text."); // Create with jQuery
var txt3 = document.createElement("p"); // Create with DOM
txt3.innerHTML = "Text.";
$("body").append(txt1, txt2, txt3); // Append the new elements
}
function afterText() {
var txt1 = "<b>I </b>"; // Create element with HTML
var txt2 = $("<i></i>").text("love "); // Create with jQuery
var txt3 = document.createElement("b"); // Create with DOM
txt3.innerHTML = "jQuery!";
$("img").after(txt1, txt2, txt3); // Insert new elements after <img>
}
$("p").remove(".test")
$("p").remove(".test, .demo");
$("button").click(function(){
$("h1, h2, p").addClass("blue");
$("div").addClass("important");
});
$("button").click(function(){
$("h1, h2, p").removeClass("blue");
});
$("button").click(function(){
$("h1, h2, p").toggleClass("blue");
});
css("propertyname");
css("propertyname","value");
$("p").css("background-color", "yellow");
css({"propertyname":"value","propertyname":"value",...});
$("p").css({"background-color": "yellow", "font-size": "200%"});