Рандомизация .click и .dblclick - PullRequest
2 голосов
/ 01 июня 2011

Я пытаюсь заставить jquery рандомизироваться, если для функционирования чего-то необходим один щелчок или двойной щелчок, но каким-то образом я застреваю во всех попытках. Я знаю, что этого не следует делать, но я изучаю отвлекающие факторы. : D

Это мой код атм:

var rnd=Math.floor(Math.random()*2)
if(rnd != 0) {
     $('.energie div.article').click(function() {

     $('#maincontent').css('height', '100%');        
     $('.energie div.article').hide(),

     $(this).next('div.detail').show();
     $('#numbersenergie div').removeClass();
     });
     };
 else {
     $('.energie div.article').dblclick(function() {

     $('#maincontent').css('height', '100%');        
     $('.energie div.article').hide(),

     $(this).next('div.detail').show();
     $('#numbersenergie div').removeClass();
     });
     };
 };

Ответы [ 3 ]

0 голосов
/ 01 июня 2011

Попробуйте этот код. Ваш не очень хорошо сформирован:

var rnd=Math.floor(Math.random()*2);
if(rnd != 0) {
     $('.energie div.article').click(function() {

         $('#maincontent').css('height', '100%');        
         $('.energie div.article').hide(),

         $(this).next('div.detail').show();
         $('#numbersenergie div').removeClass();
     });
 } else {
     $('.energie div.article').dblclick(function() {

         $('#maincontent').css('height', '100%');        
         $('.energie div.article').hide(),

         $(this).next('div.detail').show();
         $('#numbersenergie div').removeClass();
     });
 }
0 голосов
/ 01 июня 2011
//No more code redundancy - use a function :)
function handler() {
    $('#maincontent').css('height', '100%');        
    $('.energie div.article').hide();
    $(this).next('div.detail').show();
    $('#numbersenergie div').removeClass();

    //"reroll" the handler
    bind();
}

//bind your event handler here, "50/50 probability"
function bind() {
    //random whether it will be dblclick or click
    var whatHandler = Math.random() > 0.5 ? 'dblclick' :'click';

    //bind our handlers to a namespace called 'test' for easy unbinding
    $('div').unbind('.test').bind(whatHandler + '.test', handler);
}

$(function() {
   //on doc ready, bind our handler for the first time
   bind();
});

См. Упрощенную версию здесь: http://jsfiddle.net/FvScr/10/

0 голосов
/ 01 июня 2011

попробуйте

var rnd=Math.floor(Math.random()*2)

if(rnd != 0) {
     $('.energie div.article').click(function() {
        $('#maincontent').css('height', '100%');        
        $('.energie div.article').hide();
        $(this).next('div.detail').show();
        $('#numbersenergie div').removeClass();
     });
} else {
     $('.energie div.article').dblclick(function() {

        $('#maincontent').css('height', '100%');        
        $('.energie div.article').hide();

         $(this).next('div.detail').show();
        $('#numbersenergie div').removeClass();
     });
 }

У вас был;за} закрывает if и the, также a, после функции hide ()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...