【问题标题】:Jquery toggle(), hide() not working as expectedJquery toggle()、hide() 未按预期工作
【发布时间】:2010-12-13 23:48:36
【问题描述】:

我刚开始在 Drupal 环境中使用 JQuery。

我有一些缩略图,我想要它,以便当您单击一个时,更大的图片会出现在“视口”中(出现一个隐藏的 div)。可能有更好的方法来完成我正在做的事情,但我需要这样做。

这就是我所拥有的:

$(document).ready(function() {
  //$('#bigpic1').hide(); //show the first big picture always on page load
  $('#bigpic2').hide();
  $('#bigpic3').hide();

 $('a#thumb1').click(function() {
    $('#bigpic2').hide(); $('#bigpic3').hide();
    $('#bigpic3').toggle(200);
     return false;
  });
 $('a#thumb2').click(function() {
    $('#bigpic1').hide(); $('#bigpic3').hide();
    $('#bigpic2').toggle(200);
    return false;
  });
  $('a#thumb3').click(function() {
    $('#bigpic1').hide(); $('#bigpic2').hide();
    $('#bigpic3').toggle(200);
    return false;
  });

});

除了是一些丑陋的代码之外,它还不能正常工作。当页面出现时,第一张大图片不会出现,单击更多缩略图会显示正确的 div - 但从不隐藏任何 div(应该在“视口”中一次只能看到一张大图片)。

我的 HTML 看起来像这样

<table><tr>
  <td><a href="#" mce_href="#" id="thumb1">...thumb1...</td>
  <td><a href="#" mce_href="#" id="thumb2">...thumb2...</td>
  <td><a href="#" mce_href="#" id="thumb3">...thumb3...</td>
</tr>
<tr><td colspan="3">
  <!-- my "viewport" cell -->
  <!-- the big picture displays here -->
  <div  id="bigpic1">... </div>
  <div  id="bigpic2">...</div>
  <div  id="bigpic3">...</div>
</td></tr></table>

【问题讨论】:

  • 你为什么要隐藏一些并切换其他的?为什么不简单地使用 hide() 和 show(),因为您实际上是手动切换图像。
  • 您要关闭那些&lt;a&gt; 标签对吗?在您的第一个处理程序中,您应该切换#bigpic1...在这里玩的东西:jsfiddle.net/sje397/R5Fsh
  • 感谢您向我展示 JSFiddle!

标签: jquery hide toggle show


【解决方案1】:

# 选择器选择一个 id,因此您不需要其中的类型。选择器是 jQuery 最好的特性之一,所以要更加熟悉它们。 http://api.jquery.com/category/selectors/.

您甚至可以执行这样的通用方法(您需要为 元素命名)。

<table>
    <tr>
        <td>
            <a href="#" mce_href="#" name="thumb1" id="thumb1">...thumb1...</a>
        </td>
        <td>
            <a href="#" mce_href="#" name="thumb2" id="thumb2">...thumb2...</a>
        </td>
        <td>
            <a href="#" mce_href="#" name="thumb3" id="thumb3">...thumb3...</a>
        </td>
    </tr>
    <tr>
        <td colspan="3">
            <!-- my "viewport" cell -->
            <!-- the big picture displays here -->
            <div name="bigpic1" id="bigpic1">...1... </div>
            <div name="bigpic2" id="bigpic2">...2... </div>
            <div name="bigpic3" id="bigpic3">...3... </div>
        </td>
    </tr>
</table>

还有 jQuery 代码。

//on load
$(document).ready(function() {
    $('#bigpic2').hide();
    $('#bigpic3').hide();

    //this will be run when an element has a name with the text "thumb" in it
    $('[name*=thumb]').click(function() {
        //hide all big pictures (loops over every element that has a name with the text "bigpic" in it
        $('[name*=bigpic]').each(function(index, value) {
            if ($(this).is(':visible')) { //if the big pic is visible
                $(this).hide(200); //then hide it
                return false; //found the visible big pic so no need to keep looping
            }
        });
        //show the right image
        $('#bigpic' + $(this).attr('id').replace('thumb', '')).show(200);
    });
});// end on load

这是更少的代码,更具可扩展性。如果您添加/删除图像,则无需更改。 关于加载时显示的第一张图片,您是否在文档加载时尝试过$('#bigpic1').show();。哦,你不需要在函数中返回值。

【讨论】:

  • 你知道each 是一个jQuery 集合上的方法,对吗?即你不需要$.each($(foo),...) 你可以做$(foo).each(...)
  • +1 表示可扩展性,尽管对于没有 jQuery 经验的人来说可能有点神秘;)。
  • 对于一些通用的东西,你可能想看看改变类名(例如,只使用 'thumb' 和 'bigpic' 以及 jquery 的 index 方法)或者可能使用数据属性。
【解决方案2】:

首先,通过 ID 或类来识别您的“视口单元”会很方便。我不知道您文档的其余部分是如何设置的,例如:

<td id="viewport-cell" colspan="3">
  <div id="bigpic1">...1... </div>
   ...etc...
</td>

同样,也许您可​​以在缩略图链接中添加一个类以简化操作:

<a class="thumb" href="#" mce_href="#" name="thumb1" id="thumb1"></a>

设置隐藏“bigpics”的默认样式:

td#viewport-cell div { /* hide all bigpics */
  display: none;
}
td#viewport-cell div#bigpic1 { /* but show the first one */
  display: block;
}

最后,使用 jQuery:

$(document).ready( function(){
  $('table#mytable').find('a.thumb').click( function(e){
    e.preventDefault(); // suppress native click
    var bigpicid = '#bigpic'+ this.id.replace(/^thumb/i,'');
    $(bigpicid).siblings().hide().end().fadeIn('slow');
  });
});

这是一个例子:http://jsfiddle.net/redler/SDxwF/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-06
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多