Hey, I have this jQuery / Javascript piece:
$(document).ready(function() { var points = 0; var iterations = 10; var winWidth = $(window).width(); var winHeight = $(window).height(); setInterval(function() { if (iterations > 0) { var rndX = Math.ceil(Math.random() * winWidth); var rndY = Math.ceil(Math.random() * winHeight); $('div.item').remove(); $('body').append('<div class="item" style="left: '+rndX+'px; top: '+rndY+'px;"></div>'); iterations--; } else { location.href = "http://www.google.com/"; $('*').remove(); } }, 1750); $('div.item').click(function() { $(this).remove(); points = points + 1; $('#points').val(points); return false; }); });
But for some reason it $('div.item').click(function()does not start: (
$('div.item').click(function()
Any ideas?
Instead of using click, use the delegate command:
$('body').delegate('div.item', 'click', function() { $(this).remove(); points = points + 1; $('#points').val(points); return false; });
"div.item" , , . "" <body>, . , , "div.item", , .
<body>
"" , , ( ).
divs , click ...
().
. . live() . delegate()
I would suggest using jQuery . live method for the same reasons as Pointy.
Live will snap to elements as they are created.
$('div.item').live('click', function() { $(this).remove(); points = points + 1; $('#points').val(points); return false; });
Source: https://habr.com/ru/post/1785754/More articles:Invalid Content-Length sent to server? - javachanging the background color in ListView disables the highlight color - androidPython 3.1 - Memory error while fetching a large list - pythonDrupal 6: Installation Profile - drupalTips for writing a subclass of CALayer for Mac and iOS? - iosConfused how to read the xml object returned by curl in php - xmlA strange problem with a simple multi-threaded program in Java - javaHow do I structure my project to share classes between an Android client application and a JSP server application? - javaWhy is java new? - javaUpdate item in database without all columns specified in ContentValues - javaAll Articles