如何修复这个 jQuery SlideToggle?

发布于 2024-11-04 08:03:01 字数 2031 浏览 4 评论 0原文

我想做的是,当单击 dl/dt 库中的这些图像时,在一行图像下独立地滑动切换文本 div,如下所示:

dl and dt gallery

但当然我至少做错了一些事情。标记位于 jQuery 选项卡中可能会很复杂,但情况可能并非如此。 jsfiddle在这里: http://jsfiddle.net/Yfs2V/8/

我认为该函数有问题:

jQuery 函数:

$(".bkcontent").hide();
$("#bookimggallery dt").click(function() {
    $(".bkcontent").hide();
$(this).parent().children(".bkcontent").slideToggle(500); });

HTML:

<div id="bookimggallery">

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title One</dt></dl>

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title Two</dt></dl>

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title Three</dt></dl>

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title Four</dt></dl>

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title Five</dt></dl>

</div>

<div id="booktextdiv">

<div class="bkcontent">Lorum Ipsum....</div>

<div class="bkcontent">Lorum Ipsum....</div>

<div class="bkcontent">Lorum Ipsum....</div>

<div class="bkcontent">Lorum Ipsum....</div>

<div class="bkcontent">Lorum Ipsum....</div>

</div>

CSS:

#bookimggallery { width: 600px; height:200px; margin:2px; text-align: center;}

dl.gallery {width: 93px; text-align: center; float: left;}

.gallery dt { width: 80px; margin-top:2px; font-size: .7em; text-align:center;}

#booktextdiv span {display:none}

*(omitted tabs CSS for clairity)*

What I'm trying to do is independently slidetoggle text divs under a row of images when those images - in a dl/dt gallery - are clicked, like so:

dl and dt gallery

But of course I'm doing at least a few things wrong. Could be complicated that the markup is in a jQuery Tab, but that may not be the case. jsfiddle here: http://jsfiddle.net/Yfs2V/8/

I think the function is problematic:

jQuery function:

$(".bkcontent").hide();
$("#bookimggallery dt").click(function() {
    $(".bkcontent").hide();
$(this).parent().children(".bkcontent").slideToggle(500); });

the HTML:

<div id="bookimggallery">

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title One</dt></dl>

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title Two</dt></dl>

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title Three</dt></dl>

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title Four</dt></dl>

    <dl class="gallery"><dt><img alt="img" src=""></dt>
        <dt>Image Title Five</dt></dl>

</div>

<div id="booktextdiv">

<div class="bkcontent">Lorum Ipsum....</div>

<div class="bkcontent">Lorum Ipsum....</div>

<div class="bkcontent">Lorum Ipsum....</div>

<div class="bkcontent">Lorum Ipsum....</div>

<div class="bkcontent">Lorum Ipsum....</div>

</div>

the CSS:

#bookimggallery { width: 600px; height:200px; margin:2px; text-align: center;}

dl.gallery {width: 93px; text-align: center; float: left;}

.gallery dt { width: 80px; margin-top:2px; font-size: .7em; text-align:center;}

#booktextdiv span {display:none}

*(omitted tabs CSS for clairity)*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮世清欢 2024-11-11 08:03:01

这里有几个问题。我认为主要是您用来访问书籍点击的选择器,而且它的文本似乎没有以任何方式链接到书籍库项目。

我在此处稍微修改了代码

基本更改是HTML

<dl class="gallery" rel="one">

rel="blahh" 添加到每本书的 dl 中,这将对应于文本 div

<div class="bkcontent" id="one">

添加 id="blah" 到包含本书文本的 div

jQuery 更改:

$("#bookimggallery  dt").click(function() {
    $(".bkcontent").hide();
    var textid = $(this).parent('dl').attr('rel');
    $('#'+textid).slideToggle(500); 
});

  • 选择器现在会选择 div 中带有 id="bookimggallery" 的所有 dt
  • It然后从 rel="blah" 获取 id,然后滑动切换它。

    在不完全重新安排代码布局的情况下,这就是我的做法。

  • There are a couple of problems going on here. I think the main being the selectors you are using to access the click on the book and also its text doesn't appear to be linked to the book gallery item in any way.

    I have modified your code slightly here

    The basic changes are HTML:

    <dl class="gallery" rel="one">
    

    Add a rel="blahh" to the dl of each book, this will correspond to the text div

    <div class="bkcontent" id="one">
    

    Add an id="blah" to the div that contains the text for the book

    The jQuery changes:

    $("#bookimggallery  dt").click(function() {
        $(".bkcontent").hide();
        var textid = $(this).parent('dl').attr('rel');
        $('#'+textid).slideToggle(500); 
    });
    
  • The selector now selects all dt's within the div with the id="bookimggallery"
  • It then gets the id from the rel="blah", then slideToggles it.

    Without completely rearranging your code layout this is how I would do this.

  • 喜你已久 2024-11-11 08:03:01

    对于一个类,您的类是 bookimggallery 而不是 imagegallerydiv,对于另一个类,文本不是您的

    元素的子元素,因此 < code>$(this).parent().children(".bkcontent") 不包含任何元素。

    Well for one your class is bookimggallery and not imagegallerydiv, for another, the text is not a child of your <dl> elements so $(this).parent().children(".bkcontent") doesn't contain any elements.

    单挑你×的.吻 2024-11-11 08:03:01

    检查此参考,看起来您对 DL/DT 的使用/DD 略有偏差。

    考虑将您的 html 重新组织为

    <dl>
      <dt class="gallery"><img alt="img" src="">Image Title One</dt>
      <dd> description text one</dd>
    
      <dt class="gallery"><dt><img alt="img" src="">Image Title Two</dt>
      <dd> description text two</dd>
    </dl>
    

    您的 jquery 然后看起来像

    $("dd").hide();
    $("dt.gallery").click(function() {
      $("dd").hide(); // or $("dd:visible").slideToggle(500);
      $(this).next("dd").slideToggle(500);
    });
    

    Checking this reference it looks like your use of DL/DT/DD is slightly off.

    Consider reorganizing your html to

    <dl>
      <dt class="gallery"><img alt="img" src="">Image Title One</dt>
      <dd> description text one</dd>
    
      <dt class="gallery"><dt><img alt="img" src="">Image Title Two</dt>
      <dd> description text two</dd>
    </dl>
    

    Your jquery would then look like

    $("dd").hide();
    $("dt.gallery").click(function() {
      $("dd").hide(); // or $("dd:visible").slideToggle(500);
      $(this).next("dd").slideToggle(500);
    });
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文