코드 해석 좀 해주세요 ㅠㅠ
제이쿼리 책보면서 공부하고 있는데 이미지 박스 4개중에서 랜덤으로 한 곳이 당첨되는 이런 로직인데
어떻게 하다보니 쓰긴 썻는데 이해좀 하고 싶어요 ㅠㅠ
$(function() {
$(".nav").click(checkForCode);
function getRandom(num) {
var my_num = Math.floor(Math.random()*num);
return my_num;
}
var hideCode = function() {
var numRand = getRandom(4);
$(".nav").each(function(index, value){
if(numRand === index) {
$(this).append("<span id = 'has_discount'></span>");
return false;
}
});
}
hideCode();
function checkForCode() {
var discount;
if($.contains(this, document.getElementById("has_discount")))
{
var my_num = getRandom(4);
console.log(my_num);
discount = "<p>당신의 코드는 Code: "+"<strong>"+my_num+"</strong>"+"</p>";
} else {
discount = "<p>"+"아쉽네요 내일 다시 도전해주세요."+"</p>"
}
$(".nav").each(function() {
if($.contains(this, document.getElementById("has_discount")))
{
$(this).addClass("discount");
}else{
$(this).addClass("no_discount");
}
$(this).unbind('click');
});
$("#result").append(discount);
}
$(".nav").hover(
function() {
$(this).addClass("hover");
},
function() {
$(this).removeClass("hover");
});
});
댓글 4개
베스트 댓글
https://codepen.io/pen/ 사이트 가입 후,
코드를 이곳에 적고 문의하시면 답변할 때도 편리합니다.
2.
코드 관련 질문은 아래 좌표 이용해 보세요.
https://sir.kr/qa/
3.
질문내용이 길면 답변하는 사람도 지쳐용. ㅎ
긴 질문은 몇 개의 질문으로 나눠서 해보세요.
글구 질문은 질문게시판으로...
$(function() {
$(".nav").click(checkForCode);
function getRandom(num) {
var my_num = Math.floor(Math.random() * num);
return my_num;
}
var hideCode = function() {
var numRand = getRandom(4);
$(".nav").each(function(index, value) {
if (numRand === index) {
$(this).append("<span id = 'has_discount'></span>");
return false;
}
});
}
hideCode();
function checkForCode() {
var discount;
if ($.contains(this, document.getElementById("has_discount")))
{
var my_num = getRandom(4);
console.log(my_num);
discount = "<p>당신의 코드는 Code: " + "<strong>" + my_num + "</strong>" + "</p>";
} else {
discount = "<p>" + "아쉽네요 내일 다시 도전해주세요." + "</p>"
}
$(".nav").each(function() {
if ($.contains(this, document.getElementById("has_discount")))
{
$(this).addClass("discount");
} else {
$(this).addClass("no_discount");
}
$(this).unbind('click');
});
$("#result").append(discount);
}
$(".nav").hover(
function() {
$(this).addClass("hover");
},
function() {
$(this).removeClass("hover");
});
});
https://codepen.io/pen/ 사이트 가입 후,
코드를 이곳에 적고 문의하시면 답변할 때도 편리합니다.
2.
코드 관련 질문은 아래 좌표 이용해 보세요.
https://sir.kr/qa/
3.
질문내용이 길면 답변하는 사람도 지쳐용. ㅎ
긴 질문은 몇 개의 질문으로 나눠서 해보세요.
게시판 목록
자유게시판
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 공지 |
|
1주 전 | 10 | |
| 199508 |
|
3개월 전 | 465 | |
| 199507 | 3개월 전 | 324 | ||
| 199506 | 3개월 전 | 240 | ||
| 199505 | 3개월 전 | 358 | ||
| 199504 | 3개월 전 | 294 | ||
| 199503 |
비버팩토리
|
3개월 전 | 197 | |
| 199502 | 3개월 전 | 332 | ||
| 199501 |
|
3개월 전 | 247 | |
| 199500 | 3개월 전 | 513 | ||
| 199499 | 3개월 전 | 300 | ||
| 199498 | 3개월 전 | 257 | ||
| 199497 | 3개월 전 | 204 | ||
| 199496 | 3개월 전 | 422 | ||
| 199495 |
nekoieye
|
3개월 전 | 204 | |
| 199494 | 3개월 전 | 232 | ||
| 199493 | 3개월 전 | 166 | ||
| 199492 | 3개월 전 | 219 | ||
| 199491 | 3개월 전 | 222 | ||
| 199490 |
|
3개월 전 | 228 | |
| 199489 | 3개월 전 | 339 | ||
| 199488 | 3개월 전 | 306 | ||
| 199487 | 3개월 전 | 235 | ||
| 199486 | 3개월 전 | 353 | ||
| 199485 | 3개월 전 | 446 | ||
| 199484 | 3개월 전 | 342 | ||
| 199483 | 3개월 전 | 226 | ||
| 199482 |
|
3개월 전 | 482 | |
| 199481 |
|
3개월 전 | 243 | |
| 199480 |
비버팩토리
|
3개월 전 | 262 | |
| 199479 | 3개월 전 | 306 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기