2017-03-08 53 views
0

我想在最簡單的事件中生成一個隨機數點擊嵌套事件點擊;但不是一個單一的數字,我得到了很多。在嵌套點擊事件的內部點擊事件中生成一個隨機數

的代碼如下:

$("#onePl").click(function(){ 
    $("#firstP").fadeOut("slow",function(){ 
     $("#secondP").fadeIn("slow",function(){ 
      $("#theX").click(function(){ 
      $("#secondP").fadeOut("slow",function(){ 
       $("table").fadeIn("slow"); 
       **var ran=Math.random();// *I want a single random number*** 
       **console.log(ran);// I get lot of random numbers** 
          }) 

         }) 
       }) 
      }) 
     }) 
+0

您可以發佈搗鼓呢? – Happy

+0

是的,小提琴是:[小提琴](https://jsfiddle.net/cortazar11/16mxsu8r/) – user1881983

回答

0

嵌套點擊事件將具有不希望的效果的結合,因爲每次外click事件被炒魷魚,一個新的綁定內事件的增加。

你的代碼看起來應該是這樣

$("#onePl").click(function(){ 
    $("#firstP").fadeOut("slow",function(){ 
     $("#secondP").fadeIn("slow"); 
    }); 
}); 

$("#theX").click(function(){ 
    $("#secondP").fadeOut("slow",function(){ 
     $("table").fadeIn("slow"); 
     **var ran=Math.random();// *I want a single random number*** 
     **console.log(ran);// I get lot of random numbers** 
    }); 
}); 
+0

是的,那就是要點。第二次點擊第一個點的時候也是一樣的,它只產生一個隨機數。 – user1881983

+0

很高興幫助!如果此答案已解決您的問題,請點擊複選標記考慮[接受它](http://meta.stackexchange.com/q/5234/179419)。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 –