我有以下代码-
<html>
<head>
<title>Rebound</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function ShowRandom() {
var $span = $(".random");
var oldTop = 0;
var oldLeft = 0;
var leftPos = 10;
var topPos = 10;
var flag = true;
$(".random").on("click", "#lion", function(event) {
alert(event.target.id);
});
$span.fadeOut(500, function() {
var maxLeft = $(window).width() - $span.width();
var maxTop = $(window).height() - $span.height();
oldTop = topPos;
oldLeft = leftPos;
leftPos = Math.floor(Math.random() * (maxLeft + 1));
topPos = Math.floor(Math.random() * (maxTop + 1));
$span.css({ left: leftPos, top: topPos }).fadeIn(500);
});
};
ShowRandom();
setInterval(function(){
$('.random').append("<span class='lab'></span>");
ShowRandom();
}, 1500);
$('.random').delegate('.lab', 'click', function() {
$('.random').remove('.lab');
alert('single event trigger');
});
</script>
<style>
span {
display: inline-block;
position: absolute;
}
</style>
</head>
<body>
<div id="container">
<span class="random"><img src="lion.jpg" width="50px" height="50px" id="lion"></span>
</div>
</body>
</html>
单击图像时,我只需要一个警报框。但我收到多个警报框。有办法解决吗?
谢谢你