• :text : type 속성이 text인 input 태그를 선택
• :password : type 속성이 password인 input 태그를 선택
• :radio : type 속성이 radio인 input 태그를 선택
• :checkbox : type 속성이 submit인 input 태그를 선택
• :submit : type 속성이 submit인 input 태그를 선택
• :reset : type 속성이 rese 인 input 태그를 선택
• :button : type 속성이 button인 input 태그를 선택
• :image : type 속성이 image인 input 태그를 선택
• :file : type 속성이 file인 input 태그를 선택
실습1.
<script>
$(function(){
//$(":input").css("background-color", "yellow");
$(":text").css("background-color", "yellow");
$(":password").css("background-color", "red");
})
</script>
</head>
<body>
<input type="text"/><br>
<input type="password"/><br>
</body>

• :enabled : 활성 상태인 input 태그가 선택
• :disabled : 비 활성 상태인 input 태그가 선택
• :selected : select 태그 내의 option 태그 중 현재 선택되어 있는 태그
를 선택
• :checked : checked나 radio에서 현재 check 되어 있는 태그를 선택
<script>
$(function(){
$(":enabled").css("background-color", "yellow");
$(":disabled").css("background-color", "red");
})
</script>
</head>
<body>
<input type="text"/>활성 상태<br>
<input type="password"/>활성 상태<br>
<input type="text", disabled="disabled"/>비 활성 상태<br>
<input type="password", disabled="disabled"/>비 활성 상태<br>
</body>

focus : 포커스가 주어졌을 때
Blur : 포커스를 잃었을 때
on : 이벤트를 설정하는 함수
off : 설정된 이벤트를 제거하는 함수
one : 이벤트를 설정하고 이벤트가 발생했을 때 자동으로 제거
one은 이벤트 하나만, on은 이벤트를 여러개 만들 수 있는데 off를 통해 멈출 수 있음
실습2.
<script>
$(function(){
$("#a1").click(function(){
$("#a1").css("background-color", "black");
$("#a1").css("color", "white");
});
$("#a2").dblclick(function(){
$("#a2").css("background-color", "black");
$("#a2").css("color", "white");
});
$("#a3").mouseenter(function(){
$("#a3").css("background-color", "black");
$("#a3").css("color", "white");
});
$("#a3").mouseleave(function(){
$("#a3").css("background-color", "white");
$("#a3").css("color", "black");
});
$("#a4").mousedown(function(){
$("#a4").css("background-color", "black");
$("#a4").css("color", "white");
});
$("#a4").mouseup(function(){
$("#a4").css("background-color", "white");
$("#a4").css("color", "black");
});
$("#a5").hover(function(){
$("#a5").css("background-color", "black");
$("#a5").css("color", "white");
},function(){
$("#a5").css("background-color", "white");
$("#a5").css("color", "black");
});
$("#a6").focus(function(){
$("#a6").css("background-color", "blue");
});
$("#a6").blur(function(){
$("#a6").css("background-color", "red");
});
$("#a7").on("click", function(){
alert('a7');
});
$("#a8").one("click", function(){
alert('a8');
});
$("#a9").on({
click : function(){
alert('click');
},
mouseenter : function(){
$("#a9").css("background-color", "black");
},
mouseleave : function(){
$("#a9").css("background-color", "white");
}
});
function remove_event(){
$("#a7").off("click");
}
});
</script>
</head>
<body>
<h3 id="a1">click</h3>
<h3 id="a2">double click</h3>
<h3 id="a3">mouse enter/leave</h3>
<h3 id="a4">mouse down/up</h3>
<h3 id="a5">mouse hover</h3>
<input id="a6" type="text"/>
<h3 id="a7">on</h3>
<button type="button" onclick="remove_event()">이벤트 제거</button>
<br>
<h3 id="a8">one</h3>
<h3 id="a9">event setting</h3>
</body>
click 이벤트

double click 이벤트 (dblclick)

마우스 enter, leave 이벤트

마우스 down, up이벤트

마우스 hover 이벤트

focus, Blur 이벤트

on, off, one이벤트

on 클릭하면

one 클릭하면

event setting 클릭하면

마우스 enter, leave기능을 넣어주면

실습. text
<script>
function getA1(){
var str = $("#a1").text();
alert(str);
}
function getA2(){
var str = $("#a2").text();
alert(str);
}
function setA3(){
$("#a3").text("문자열 설정");
}
function setHtml(){
$("#a3").text("<a href='http://apple.co.kr'>apple</a>");
}
</script>
</head>
<body>
<div id="a1">a1 문자열</div>
<button onclick="getA1()">a1 문자열 가져오기</button>
<div id="a2">
<a href="http://www.google.co.kr">google</a>
</div>
<button onclick="getA2()">a2 문자열 가져오기</button>
<div id="a3"></div>
<button onclick="setA3()">a3 문자열 가져오기</button>
<button onclick="setHtml()">a3 html 설정하기</button>
</body>

a1 문자열 가져오기 버튼을 누르면 알림창 출력

a2 문자열 가져오기 버튼을 누르면 알림창 출력

a3 문자열과 html버튼 클릭 시

실습
<script>
function getHtml(){
var html = $("#a1").html();
alert(html);
}
function setHtml(){
$("#a1").html("<a href='http://apple.co.kr'>apple</a>");
}
function getA1(){
var value = $("#a2").val();
alert(value);
}
function setA1(){
$("#a2").val("1234");
}
</script>
</head>
<body>
<div id="a1">
<a href="http://www.google.co.kr">google</a>
</div>
<button onclick="getHtml()">html 가져오기</button>
<button onclick="setHtml()">html 셋팅하기</button>
<br>
<input type="text" id="a2"/>
<br>
<button onclick="getA1()">value값 가져오기</button>
<button onclick="setA1()">value값 설정하기</button>
</body>

html 가져오기를 누르면 알림창 출력

text와 다르게 html을 사용하면 안에 기능을 사용할 수 있음

val 이벤트

입력 후 가져오기를 누르면 알림창 출력

코드로 설정해둔 값을 설정하기 버튼으로 설정 후 가져오기 눌러도 알림창이 출력된다.

<script>
function getAttr(){
var src = $("#a1").attr("src");
var width = $("#a1").attr("width");
var height = $("#a1").attr("height");
var id = $("#a1").attr("id");
// 가지고 온 값 나타내기
$("#result").html("src : "+ src + "<br>"
+ "width : " + width + "<br>"
+ "height : " + height + "<br>"
+ "id : " + id + "<br>");
}
function setAttr(){
$("#a1").attr("width", 544);
$("#a1").attr("height", 184);
}
</script>
</head>
<body>
<img src="image/google.png" width="272" height="92" id="a1"/>
<br/>
<div id="result"></div>
<button onclick="getAttr()">속성 읽어오기</button>
<button onclick="setAttr()">속성 설정하기</button>
</body>

<script>
function append1(){
$("#a1").append("<p>새롭게 추가한 p1</p>");
}
function append2(){
var p = $("<p>"); // 태그를 p 변수에 넣음 -> 객체
p.text("새롭게 추가한 p2");
$("#a1").append(p);
}
function prepend1(){
$("#a1").prepend("<p>새롭게 추가한 p3</p>");
}
function prepend2(){
var p = $("<p>"); // 태그를 p 변수에 넣음 -> 객체
p.text("새롭게 추가한 p4");
$("#a1").prepend(p);
}
</script>
<style>
#a1{
border : 1px solid black;
}
</style>
</head>
<body>
<div id="a1">
<p>p 태그</p>
</div>
<button onclick="append1()">HTML 코드를 뒤에 추가</button>
<button onclick="append2()">HTML 객체를 뒤에 추가</button>
<button onclick="prepend1()">HTML 코드를 앞에 추가</button>
<button onclick="prepend2()">HTML 객체를 앞에 추가</button>
</body>

추가 버튼을 눌러보면

<script>
function after1(){
$("#a1").after("<p>새롭게 추가한 p태그 1</p>")
}
function after2(){
var p = $("<p>")
p.text("새롭게 추가한 p태그 2");
$("#a1").after(p);
}
function before1(){
$("#a1").before("<p>새롭게 추가한 p태그 3</p>")
}
function before2(){
var p = $("<p>")
p.text("새롭게 추가한 p태그 4");
$("#a1").before(p);
}
</script>
<style>
#a1{
border : 1px solid black;
}
</style>
</head>
<body>
<div id="a1">
<p>p 태그</p>
</div>
<button onclick="after1()">HTML 코드를 뒤에 추가</button>
<button onclick="after2()">HTML 객체를 뒤에 추가</button>
<button onclick="before1()">HTML 코드를 앞에 추가</button>
<button onclick="before2()">HTML 객체를 앞에 추가</button>
</body>

버튼을 눌러보면

<script>
function remove_a2(){
$("#a2").remove();
}
function empty_a1(){
$("#a1").remove();
}
</script>
<style>
#a1{
border : 1px solid black;
}
</style>
</head>
<body>
<div id ="a1">
<p>p 태그</p>
<p id="a2">id가 a2인 p태그</p>
</div>
<button onclick="remove_a2()">a2 제거</button>
<button onclick="empty_a1()">a1 내부의 모든 태그 제거</button>
</body>



jQuery 이벤트를 배웠는데, 자바스크립트를 통해 학습을 할 때와 비교하면 조금 더 간편한 느낌이 있었다. 아직 복잡한 코딩을 안해서 잘 모르겠지만 아직까지는 자바스크립트보다 더 편하고 쉬웠다. 그런 만큼 지나간 자바스크립트 복습도 중요할 것 같다.