简体   繁体   English

jQuery的隐藏/显示奇怪的故障

[英]Jquery hide/show weird glitch

This would be easier with a jsfiddle, but as god is my witness something is wrong with jsfiddle and none of my js is working, but they work on my localhost though so yeah. 使用jsfiddle会更容易,但是作为上帝,我是目击者,jsfiddle出了点问题,我的js都没有工作,但是它们可以在我的本地主机上工作,尽管如此。

So let me explain i have a div called topBar. 因此,让我解释一下,我有一个名为topBar的div。 Its hidden on dom load. 它隐藏在dom负载上。 I have a div called toggle_bar When toggle_bar is clicked, jquery hides toggle_bar and shows topBar 我有一个名为toggle_bar的div当单击toggle_bar时,jQuery隐藏了toggle_bar并显示topBar

But the problem im having is that after i click toggle_bar, topBar is shown but i move my mouse a bit and BAM! 但是我遇到的问题是,在我单击toggle_bar之后,显示了topBar,但是我将鼠标移了一点并且BAM! topBar is gone. topBar不见了。 I dont know why this is happening 我不知道为什么会这样

here is my code Jquery 这是我的代码jQuery

$("#topBar").hide();

$("#toggle_bar").live("click",function (){
   $("#toggle_bar").hide();
   $("#topBar").show();
});

HTML HTML

    <div class='toggle_bar'>
        <a href='' id="toggle_bar" class="toggle_bar_class"></a>
    </div>
<div id="topBar" class="topBar" >
    <div class="bar_frame">
        <div class="plogo">
           Page logo bla bla bla
        </div>
        <div class="controls">
          Notifications bla bla bla
        </div>
        <div class="nav_bar_frame">
        <div class="float_left_bar"> 
        </div>
        <div class="float_right_bar">
        </div>
</div>
    </div>
</div>

PS: For the toggle_bar:a , ive used css to setup an image as href. PS:对于toggle_bar:a,我使用css将图像设置为href。 :D :d

toggle_bar上的href应该包含#否则您应该在click处理程序中停止该事件。

Is there a reason why you are hiding on load rather than setting to display none in the styles? 是否有原因为什么您隐藏了加载而不是设置为不显示任何样式?

OriginalSyn is right, you could write it like this... OriginalSyn是正确的,您可以这样写...

$("#toggle_bar").live("click",function () {
    $(this).hide();
    $("#topBar").show();
    return false;
});

我建议(如果您使用的是最新版本的jQuery来代替.live()方法,则使用.on()或.delegate()方法。我将向您推荐这篇非常出色的文章)解释差异。

Heres a working fiddle . 这是工作中的小提琴 You should also be using 您还应该使用

.on

instead of 代替

.live

It was deprecated as of 1.7. 1.7开始不推荐使用。

It seems like you need to prevent the link from traveling to the URL. 似乎您需要阻止链接访问URL。 Here is an example of how to change your click event handler to do so. 这是一个如何更改点击事件处理程序的示例。

http://jsfiddle.net/dp3T2/ http://jsfiddle.net/dp3T2/

$("#toggle_bar").live("click",function (e){
  $("#toggle_bar").hide();
  $("#topBar").show();
  e.preventDefault();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM