강의 4개
결과가 +이면, 오른쪽에 있는(b)를 왼쪽으로 보낸다.
결과가 +이면, b를 왼쪽으로 보낸다.
결과가 -이면, b를 오른쪽으로 보낸다.
- 예시
- 오름차순 정리
- 7-3=4 결과는 +이다. 그러므로 3을 왼쪽으로 보낸다.
const arr1 = [7,3,5]; arr1.sort(function(a,b){ return a-b; });
- 예시2
- cba순 정리
- 's'는 'a'보다 크다.
- 그러므로 's'를 왼쪽으로 보낸다.
const arr1 = ['a','s','d']; arr1.sort(function(a,b){ return a < b : 1 ? -1 });
변수를 ${}로 감싼다.
`abc${변수}def`
cf) 따옴표 문자열에 js변수 추가
"abc"+ 변수 +"def"
$(".card-group").html("");
for (let i = 0; i < lowPriceProducts.length; i++) {
$(".card-group").append(`<div class="card">
<img src="https://via.placeholder.com/600" />
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p>가격 : <span class="card-price">70000</span>원</p>
<button class="btn btn-danger">주문하기</button>
</div>
</div>`);
$(".card-title").eq(i).text(lowPriceProducts[i].title);
$(".card-price").eq(i).text(lowPriceProducts[i].price);
}
첫번째 : 일단 html을 없앤다.
$(".card-group").html("");
두번째 : 원하는 부분에 html을 넣는다.
for (let i = 0; i < lowPriceProducts.length; i++) { $(".card-group").append(`<div class="card"> <img src="https://via.placeholder.com/600" /> <div class="card-body"> <h5 class="card-title">Card title</h5> <p>가격 : <span class="card-price">70000</span>원</p> <button class="btn btn-danger">주문하기</button> </div> </div>`); }
const arr = [26, 28, 30, 32];
arr.forEach(function(a){
`<div>${a}</div>`
})
<script>
document.getElementById('test').innerHTML = '안녕'
</script>
<p id="test">임시글자</p>
<script>
$(document).ready(function(){
document.getElementById('test').innerHTML = '안녕'
});
</script>
<p id="test">임시글자</p>
document.addEventListener('DOMContentLoaded', function() {
//DOM생성이 완료되었을 때 실행할 코드
});
DOM생성
뿐만아니라 파일이 로드
됐는지 체크한다.$('img').on('load', function(){
//이미지가 다 로드가 되었을 경우 실행할 코드
});
로드
됐는지 체크한다.$(document).on('load', function(){
//이미지가 다 로드가 되었을 경우 실행할 코드
});
$.ajax().done()
$(".ajax-btn").on("click", function () {
$.ajax({
url: "https://codingapple1.github.io/hello.txt",
type: "GET",
}).done(function (e) {
$(".ajax-text").text(e);
}).fail(function () {})
.always(function () {});
});