代码:
$(document).ready(function(){ $("#main_div").bind('click', function(e){ var x = event.pageX-document.getElementById("main_div").offsetLeft; var y = event.pageY-document.getElementById("main_div").offsetTop; if(...) { $("#container-5").css({"top":y,"left":x}); $(this).unbind('click'); e.stopPropagation(); } else if(...) { .... }); });当我单击main_div时,它将检查是否有功能并继续.容器5的CSS也被更改.现在,一旦更改了CSS,我就不希望它在main_div点击次数上保持不变.所以我尝试了$(this).unbind('click');,但这没有帮助,因为我希望main_div可以被多次点击,以便我可以执行其他功能.非常感谢您的帮助.
When i click on main_div it checks for if function and proceeds. Css of container-5 is changed too. Now once the css is changed i don't want it to keep changing on main_div clicks. So i tried $(this).unbind('click'); But this doesnt help because i want main_div to be clicked as many times as i want so that i can perform other functionalities. Help really appreciated.
推荐答案只需在回调函数内添加一条附加的if语句,该语句将在第一时间为true,之后永远为false:
Just add an additional if statement inside the callback function that will be true the very first time and false forever afterwards:
var hasBeenClicked = false; $("#main_div").bind('click', function(e){ if(!hasBeenClicked){ hasBeenClicked = true; ... //the rest of your code ... } });更多推荐
只需单击一次即可更改CSS位置
发布评论