简体   繁体   English

使用点击处理程序(JS)隐藏和显示div在Firefox中不起作用

[英]Hiding and showing divs using click handler (JS) not working in Firefox

I have a click function that captures a clicked link and hides and un-hides some divs based one the following script: 我有一个click函数,可捕获单击的链接,并基于以下脚本隐藏和取消隐藏某些div:

$(".link").bind('click', function() {
  //link handler
  event.preventDefault();  //prevents url from changing
  // $(window).resize();  //testing something else
  var value = $(this).attr("href");  //grab hash link of li that was clicked and call it 'value'
  if ($(value).hasClass("active")) {
    return false  //if you click on the link that's already visible, don't do anything
  } else {
    $(value).removeClass("hidden").parent().find(".active").addClass("hidden").removeClass("active");  //unhide the div we want, find current active div and hide it
    $(value).addClass("active");  //classify the now visible div as active
  };
});

My .hidden class hides a div using display:none, and the .active class is my way of identifying the currently showing div. 我的.hidden类使用display:none隐藏div,而.active类是我识别当前显示的div的方式。

It works perfectly in Chrome, but no dice in FireFox or Internet Explorer. 它在Chrome浏览器中完美运行,但在FireFox或Internet Explorer中没有骰子。 Any help is much appreciated! 任何帮助深表感谢!

EDIT: Here's my css: 编辑:这是我的CSS:

.hidden {
  display:none

} }

.active doesn't have any rules, i just use it to identify the visible div. .active没有任何规则,我只是用它来识别可见的div。

Here's a fiddle: http://jsfiddle.net/j7df7/ 这是一个小提琴: http : //jsfiddle.net/j7df7/

appears to be the event.preventDefault(); 似乎是event.preventDefault(); that is stopping execution in firefox. 这将停止在Firefox中执行。 Comment that out and test again. 发表评论并再次测试。 Also, as sheriffderek mentioned, you probably should pass in an event so that your code knows what it is. 另外,如sheriffderek所述,您可能应该传递一个事件,以便您的代码知道它是什么。

Is this what you looking for? 这是您要找的东西吗?

$(".link").click(function () {
var value = $(this).attr("href"); //grab hash link of li that was clicked and call it 'value'

if(!$(value).hasClass('active'))
{
    $(value).addClass('active').removeClass('hidden');
    $(value).siblings('div').addClass('hidden').removeClass('active');
}


});

Here's a working JSFiddle update that works with; 这是一个可以使用的JSFiddle更新。 Chrome, Internet Explorer and FireFox. Chrome,Internet Explorer和FireFox。 I've steamlined your code, for example, instead of: 例如,我简化了您的代码,而不是:

$(value).removeClass("hidden").parent().find(".active").addClass("hidden").removeClass("active")

There is now just: 现在只有:

 $(".active").attr('class', 'hidden');

I have also changed your 我也改变了你

$(".link").on('click', function () {

To just (FireFox requires you put put an 'event' parameter in your code in order for it to work) 只是(FireFox要求您在代码中放置一个“事件”参数才能使其正常工作)

$(".link").click(function (event) {

Anyway, here's the JSFiddle: http://jsfiddle.net/j7df7/3/ 无论如何,这是JSFiddle: http : //jsfiddle.net/j7df7/3/
Here's a version where the divs have 2 classes, so you can keep one class for style and another for hiding: http://jsfiddle.net/j7df7/5/ 这是一个div具有2个类的版本,因此您可以保留一个类的样式,而另一个保留样式: http : //jsfiddle.net/j7df7/5/

WHAT ABOUT THIS! 那这个呢! I'm not sure if this will fit for you --- but I recently got turned onto data-* and it's awesome... try this out. 我不确定这是否适合您---但是最近我转向了data- *,它很棒...尝试一下。 It's pretty darn eloquent..... FIDDLE 真是太好说了 ..... FIDDLE

HTML HTML

<ul class='menu'>
    <li><a href='#' data-display-item='.one'>Link 01</a></li>
    <li><a href='#' data-display-item='.two'>Link 02</a></li>
    <li><a href='#' data-display-item='.three'>Link 03</a></li>
</ul>

<div class="SPA-block one active-page">
    Div 01 (Active to start)
</div>

<div class="SPA-block two">
    Div 02
</div>

<div class="SPA-block three">
    Div 03
</div>

CSS CSS

.SPA-block {
    display: none;
}

.active-page {
    display: block;
}

jQuery jQuery的

$('.menu a').on('click',function() {
    $('.SPA-block').removeClass('active-page'),
    $($(this).data('display-item')).addClass('active-page');
    return false;
});

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

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