【问题标题】:jQuery IE 8 show / hide is not workingjQuery IE 8 显示/隐藏不起作用
【发布时间】:2012-04-20 22:14:00
【问题描述】:

我有一个问题,为什么它在 IE 8 中工作

$(function() {
    $('.title .hide').showContent();
});

$.fn.showContent = function() {
    return this.each(function() {
        var box = $(this);
        var content = $(this).parent().next('.content');

        box.toggle(function() {
            content.slideUp(500);
            $(this).css('background-position', 'right bottom');
        }, function() {
            content.slideDown(500);
            $(this).css('background-position', 'right top');
        });

    });
};

这不起作用?

$(function() {
    $('.title .hide.show').hideContent();
});

$.fn.hideContent = function() {
    return this.each(function() {
        var box = $(this);
        var content = $(this).parent().next('.content');

        box.toggle(function() {
            content.slideDown(500);
            $(this).css('background-position', 'right top');
        }, function() {
            content.slideUp(500);
            $(this).css('background-position', 'right bottom');
        });

    });
};

我希望这两个选项工作,但我不知道为什么第二个在 IE 8 中不起作用,我希望有人能帮助我。

【问题讨论】:

  • 这两个函数有可能互相干扰吗?您在调用 hideContent() 的所有元素上调用 showContent(),如果您使用唯一类识别元素会发生什么?
  • 不,我有 并将 js 更改为 .title .hide 和 .title .show但仍然没有运气......
  • 你能创建一个jsFiddle问题的演示吗?
  • 嗨,安东尼,这是一个小提琴jsfiddle.net/wyJXr,但这很好用。我认为它在我的代码中有所不同导致错误。

标签: jquery internet-explorer-8 hide show collapse


【解决方案1】:


Jquery 内置了 hide() 和 show() 等函数。
试试看。
例如:$('input#id').hide();
同样使用类选择器也不是一个好主意,因为性能会受到影响。
尝试使用 id。

【讨论】:

    【解决方案2】:

    如果您使用的是 jquery,请使用下面的 javascript, 它可能对你有用

    jQuery(this).hide();
    jQuery(this).show();
    

    【讨论】:

      【解决方案3】:

      看起来您只是为一个具有 show 类的元素调用 hideContent(),而该元素在 hide 类中,而该类在 title 类中。 .hide.show 是两个不同的嵌套类。

      .title 。隐藏 .show

      jQuery 正在寻找一个带有 .show in a .hide in a .title 的元素

      如果您要切换而不是将 hideContent() 的第二行替换为:

      $('.title .show').hideContent();
      $('.title .hide').hideContent();
      

      【讨论】:

      • 啊,这不起作用: 隐藏元素?
      • 这是不正确的。 jQuery 选择器正在寻找具有 showhide 类的元素,该元素位于具有 title 类的元素中。
      • 我已经更改了它,但它现在仍在 IE 8 中工作...显示和隐藏工作正常,但隐藏和显示不工作。
      • 如果你想要一个按钮/链接来切换然后应用一个不同的类,在 jQuery 中只需使用 toggle();隐藏和显示。
      【解决方案4】:

      我猜这是 IE8/jQ 中的一个错误。 试试:

      $(id_to_hide).css("display", "none");
      $(id_to_show).css("display", "inline-block");
      

      【讨论】:

        猜你喜欢
        • 2012-09-25
        • 2012-12-29
        • 2010-10-24
        • 2011-03-06
        • 2012-03-11
        • 1970-01-01
        • 1970-01-01
        • 2012-11-01
        相关资源
        最近更新 更多