1
当我点击一个图像时,我的基本功能会弹出一个弹出窗口,并允许您浏览好友列表,并将页面上的当前图片与弹出窗口中的图片进行交换。交换工作正常,但我的功能似乎多次触发 - 我知道这一点,因为d变量多次出现在控制台中。在第一次使用时,它显示一次。在第二个,它显示了两次,等等。jQuery函数循环或不结束
我想它与$('#friendlist li')有关。单击主循环内的函数,并且它不是“关闭“一旦图片被替换,或者与变量范围有关。
$('img[name=pop]').click(function(e) {
//pop up
var p = $(this);
var position = p.position();
var offset = p.offset();
$('#friendlist').css("left", position.left + $(this).width());
$('#friendlist').css("top", 0);
$('#friendlist').toggle();
//console.log(this);
//console.log(position.top + " " + offset.top + " " + e.pageY);
//listener for the friendslist, retrieves the uid of the friend chosen
var pointer = $(this).attr("id");
var c = 0;
console.log("before c: " + c);
$('#friendlist li').click(function(){
var d = 0;
console.log("inner d: " + d);
if(d < 1){
pictureReplace(pointer , $(this).attr("id")); //function that swaps the img
$('#friendlist').hide();
d++;
}
});
});
这里是页面的基本HTML:
<div id="main_page">
<div>
<img src="https://graph.facebook.com/<?php echo $friendList[0]['id']; ?>/picture" name="pop" id="friend1" />
<?php echo $friendList[0]['name']; ?>
</div>
<div>
<img src="img/<?php echo $friendList[1]['id']; ?>" name="pop" id="friend2" />
<?php echo $friendList[1]['name']; ?>
</div>
<div>
<img src="img/<?php echo $fb_me['id']; ?>" />
<?php echo $me['name']; ?>
</div>
<div>
<img src="img/<?php echo $friendList[2]['id']; ?>" name="pop" id="friend3" />
<?php echo $friendList[2]['name']; ?>
</div>
<?php //create the friends list for the search ?>
<div id="friendlist">
<div>People</div>
<input type="text" id="fsearch" name="fsearch" class="" />
<ul id="friends_ul">
<?php
$li_num = 1;
foreach($friendList as $key => $val){
?>
<li id="li_id_<?php echo $li_num; ?>">
<img src="img/<?php echo $val['id']; ?>" />
<label><?php echo $val['name']; ?></label>
<input type="hidden" name="hiddenid" value="<?php echo $val['id']; ?>">
</li>
<?php
$li_num++;
}
?>
</ul>
</div>
</div>