开发者

jQuery: run only once

Yes i have an fancybox. It auto opens if ?mode=new

What happens on ?mode=new is:

<script>
$(document).ready(function(){
$('#newPM').click();
});
</script> 

It opens fine. But when you then click on the element, it keeps closi开发者_StackOverflow社区ng/open, because this is activated. So how can i only run this once?


Do you mean it keeps opening and closing the fancybox? You can do

$("#newPM").unbind("click"); 

This will remove the onclick event so it doesn't get triggered anymore.


A better solution is to refactor your code to not emulate the click event of another element.

Emulating human interaction to shorthand your code sounds like a great idea, until you forget that something is dependent on that "click" handler.

what you really shoudl to is have something like

<script>
$(document).ready(function(){
   'check for url variable: 
   if (urlVar(something)) {
       doSomething();
    }
   $('#newPM').click(doSomething);
});
function doSomething(e) {
 'actionable code here
}
</script> 

This way your actions and your event handlers are independent from each other.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜