HeadFirst jQuery 学习笔记一

cument).ready(function(){

 2     $(".guess_box").click(checkForCode);
 3     function getRandom(num){
 4         var my_num = Math.floor(Math.random()*num);
 5         return my_num;    
 6     }
 7     var hideCode = function(){
 8         var numRand = getRandom(4);
 9         $(".guess_box").each(function(index,value){
10             console.log(numRand+"nr");
11             console.log(index+"ix");
12             if(numRand == index){
13                 $(this).append("<span id='has_discount'></span>");
14                 return false;    
15             }
16         });    
17     }
18     hideCode();
19     function checkForCode() {
20         var discount;
21         if($.contains(this,document.getElementById("has_discount")))
22         {
23             var my_num = getRandom(5);
24             discount = "<p>Your Discount is"+my_num+"%</p>"    
25         }else{
26             discount = "<p>Sorry,no discount this time!</p>"    
27         }
28     $(".guess_box").each(function(){
29         if($.contains(this,document.getElementById("has_discount")))
30         {
31             $(this).addClass("discount");
32         }else{
33                 $(this).addClass("no_discount");
34         }
35         $(this).unbind();
36     });
37     $("#result").append(discount);
38     }
39     $(".guess_box").hover(
40     function () {
41     $(this).addClass("hover");    
42     },
43     function (){
44     $(this).removeClass("hover");    
45     });    
46 });

猜你喜欢

转载自www.cnblogs.com/jieshan/p/9620004.html
今日推荐