dongmi4809 2017-04-15 15:18
浏览 47

onLoad单击功能 - 随机

Goal: Once the page is loaded a random area on the screen is clicked. Not a certain button or element, just a random click.

Maybe add an if function that if the click is successful then do another function?

  • 写回答

2条回答 默认 最新

  • douketangyouzh5219 2017-04-15 15:31
    关注

    using jquery, an approach can be...

    var posx = (Math.random() * ($(document).width())).toFixed();
    var posy = (Math.random() * ($(document).height())).toFixed();
    
    $('body').append($("<div class=myclass style=left:"+posx+"px;top:"+posy+"px;>Hello from random!</div>"));
    
    
    $( ".myclass" ).click(function() {
      alert( "random area clicked." );
    });
    .myclass{
    
    position:absolute;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

    </div>
    
    评论

报告相同问题?