开发者

How to tell JavaScript to do nothing?

I have a small chunk of code, like this:

$("div.footerMenu li").click(
    function () {
        $("div.onScreen").hide();
        $(this).children("div.onScreen").fadeIn('fast');    
    },function(){
        $("div.onScreen").hide();
});//click

And when I click on <li> the div .onScreen shows nicely, but when i click on this div, that just showed up the functions is hiding in and showing again, but I don't want it to execute this function again. So my question is: How can I somehow "detach/exclude/hide" this div from Javascript?

update:

The thing is that with this method and with others with .one() the rest of menu is not working. There is the site with the problem here . I want this div that shows up stay there, when I click on it, but when I click on their items <li> I want to other div's (submenus) to show up (warning - big images on that site).

The html looks like this开发者_如何转开发:

<div class="footerMenu"> <ul> <li>HOME<div class="onScreen"><div style="padding:50px;"><img src="fillTxt.png"></div></div></li> <li>PLENER<div class="onScreen"> <div style="padding:50px;"><img src="fillTxt2.png"></div></div> </li> <li>STUDIO<div class="onScreen"> <div style="padding:50px;"><img src="fillTxt.png"></div></div> </li> <li>INNE<div class="onScreen"> <div style="padding:50px;"><img src="fillTxt2.png"></div></div> </li> </ul> </div>


The simple solution is:

$('div.footerMenu li').unbind('click');

But should you have multiple click handlers on the selector, you may want to only remove one at a time. The way to do that is to store a reference to the function being passed:

function hideItem()
{
  ...code...
  //unbind the click event
  $(this).unbind('click', hideItem);
}

$('div.footerMenu li').click(hideItem);


If you want to handle an event only once, you can use the one() method:

$("div.footerMenu li").one("click", function() {
    $("div.onScreen").hide();
    $(this).children("div.onScreen").fadeIn("fast");    
});


You can use .one():

$("div.footerMenu li").one('click', function(){
    things_to_happen_only_once();
    // unbinding happens automatically
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜