[TIL] 2023-03-14
html파일 or js파일 별도로 작성도 가능
body 태그 안에 작성
<script></script>
함수
Math.random(); -> 0~1미만의 실수 랜덤 출력
parseInt(); -> 정수형으로 변환
반복문
for (시작; 끝; 증감;){
반복코드
}
while (조건) {
반복코드
}
.length -> 배열의 길이
.sort(); -> 배열 정렬(디폴트 값은 앞자리 크기순서)
.sort((a,b)=>a-b); -> 오름차순 정렬
조건문
if (조건) {참일경우실행}
.indexOf -> 값이 있으면 위치인덱스, 없으면 -1
DRY 지양하기
var content = document.getElementById('jasoseol').value;
console.log(content); -> content.length(글자수)
document.getElementById().value
<span id="count">(0/200)</span>
<script>
var content = document.getElementById('jasoseol').value;
document.getElementById('count').innerHTML = '('+content.length+'/200)';
</script>
document.getElementById().innerHTML
<textarea 이벤트=이벤트핸들링></textarea>
if(content.length>200){
content = content.substring(0,200);
document.getElementById('jasoseol').value = content;
}
function counter() {
var content = document.getElementById('jasoseol').value;
if(content.length>200){
content = content.substring(0,200);
document.getElementById('jasoseol').value = content;
}
document.getElementById('count').innerHTML = '('+content.length+'/200)';
}
counter();
console.log($('#content').val());
function hell() {
console.log('hello');
}
//.click()
$('#click').click(hell);
-> #click은 html 태그를 가르키고, .click(hell)은 위 함수를 가르킨다
$('#click').click(function(){
console.log('hello');
});
.fadeIn
나타내기
.animate
$('#spit').animate({left: '+=250'});
$('#spit').fadeOut(function() {
hp -= 1;
$('#hp').text('hp:'+hp);
});
-> 콜백함수: 동작이 모두 끝난 후 다음 동작 실행
<script>
var hp = 3;
$('#drone').click(function(){
$('#spit').fadeIn();
$('#spit').animate({left: '+=250'});
$('#spit').fadeOut(function() {
hp -=1;
$('#hp').text('HP: '+hp);
if(hp == 0){
$('#bunker').fadeOut();
}
});
$('#spit').css({left: 150});
});
</script>
var person = {
//키 name: 'jocoding', //값(value)
//속성명 sayHello: function() { console.log('hello'); } //속성값
}입력하세요
-> 배열,객체,함수(매세드)도 입력 가능