只需单击一次即可更改CSS位置

编程入门 行业动态 更新时间:2024-10-28 15:29:22
本文介绍了只需单击一次即可更改CSS位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

代码:

$(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位置

本文发布于:2023-10-26 08:12:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:只需   单击   位置   CSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!