제이쿼리 책보면서 공부하고 있는데 이미지 박스 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개
베스트 댓글
글구 질문은 질문게시판으로...
$(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");
});
});
게시글 목록
| 번호 | 제목 |
|---|---|
| 1717629 | |
| 1717626 | |
| 1717625 | |
| 1717621 | |
| 1717619 | |
| 1717611 | |
| 1717610 | |
| 1717609 | |
| 1717607 | |
| 1717601 | |
| 1717598 | |
| 1717591 | |
| 1717590 | |
| 1717583 | |
| 1717575 | |
| 1717572 | |
| 1717568 | |
| 1717566 | |
| 1717549 | |
| 1717545 | |
| 1717533 | |
| 1717512 | |
| 1717511 | |
| 1717508 | |
| 1717495 | |
| 1717479 | |
| 1717473 | |
| 1717470 | |
| 1717463 | |
| 1717452 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기