<body>
<div class="welcome"></div>
<div class="welcome"></div>
<script>
// $('.welcome') : 제어 대상
// .method() : 명령
$('.welcome').html('hello world! coding everybody!!!');
$('.welcome').css('background-color','tomato');
</script>
</body>
id값이나 class값을 태그 이름과 함께 명시하여 적용하는 형태
셀렉터가 지정하는 대상을 자세하게 명시할 수 있다.
태그, 클래스, 아이디를 독립적으로 명시할 때 보다 우전적으로 적용된다.
두 조건을 모두 만족해야한다.
tag.class
tag#id
<body>
<input type="button" id="pure" value="pure" />
<input type="button" id="jQuery" value="jQuery" />
<script>
// pure : javascript
// 버튼클릭시 alert, 'pure'
let target = document.getElementById('pure');
target.addEventListener('click', function(){
alert('pure');
});
// jQuery : jquery
// 버튼클릭 alert, 'jQuery'
$('#jQuery').on('click',function(){
alert('jQuery');
});
</script>
</body>
한번 선택한 대상에 대해서 연속적인 제어를 할 수 있다.
코드가 간결해진다.
<body>
<a id="tutorial" href="http://jquery.com" target="_self">jQuery</a>
<script>
// attr : 속성
//$('#tutorial').attr('href', 'http://www.naver.com');
//$('#tutorial').attr('target','_blank');
$('#tutorial').attr('href', 'http://www.naver.com')
.attr('target','_blank')
.attr('color','tomato');
</script>
</body>
$("#btn").click(function(event){
// 실행할 내용
});
// bind : 이벤트를 특정 요소에 연결하는 것을 이벤트 바인딩
$("#btn").bind("click", function(event){
// 실행할 내용
});
$("#btn").on("click",function(event){
// 실행할 내용
});
<body>
<script>
function clickHandler(){
alert('thank you');
}
$(document).bind('ready', function(){
$('#click_me').bind('click', clickHandler);
})
</script>
<input id="click_me" type="button" value="click me" />
</body>
-------------------------------------------------------
<body>
<script>
function clickHandler(){
alert("event helper");
}
// event helper
$(document).ready(function(){
$("click_me").click(clickHandler);
});
</script>
<input type="button" id="click_me" value="click me" />
</body>
-------------------------------------------------------
<body>
<h1>on()</h1>
<p>여기를 클릭해 주세요</p>
<script>
$('p').on("click",function(){
alert("문장이 클릭되었습니다.");
});
</script>
</body>