jQuery SlideToggle 一次一个 div 而不是全部独立

发布于 2024-11-04 04:32:57 字数 797 浏览 4 评论 0原文

我使用下面的函数来切换 div,通过它,任何一个条目内容 div 都可以独立打开或关闭。

如果任何时候只打开一个条目内容 div,那就太好了。单击关闭的条目标题 div 将关闭任何其他条目内容 div,然后打开单击的那个。我需要保留 html 标记,因为它是由 Wordpress 动态创建的。这可能吗?

功能:

jQuery(document).ready(function($) {
$(".entry-content").hide('slow');
$(".entry-title").click(function() {
$(this).parent().children(".entry-content").slideToggle(500); });
});

html

<div class="entry-post">

   <h2 class="entry-title">Post Title 1</h2>

   <div class="entry-content">Lorem Ipsum...

</div></div>

   <h2 class="entry-title">Post Title 2</h2>

   <div class="entry-content">Lorem Ipsum...

</div></div>

More of the same....

</div>

I'm using the function below that toggles divs, and with it, any one of the entry-content divs can be opened or closed independently of all.

What would be great is if only one entry-content div would be open at any time. Clicking a closed entry-title div would close any other entry-content div and then open the one clicked. I need to stay with the html markup, as it is created dynamically by Wordpress. Is this possible?

Function:

jQuery(document).ready(function($) {
$(".entry-content").hide('slow');
$(".entry-title").click(function() {
$(this).parent().children(".entry-content").slideToggle(500); });
});

html

<div class="entry-post">

   <h2 class="entry-title">Post Title 1</h2>

   <div class="entry-content">Lorem Ipsum...

</div></div>

   <h2 class="entry-title">Post Title 2</h2>

   <div class="entry-content">Lorem Ipsum...

</div></div>

More of the same....

</div>

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

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

发布评论

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

评论(3

百善笑为先 2024-11-11 04:32:57

每次单击时将其全部关闭即可:

jQuery(document).ready(function($) {
$(".entry-content").hide('slow');
$(".entry-title").click(function() {
    $(".entry-content").hide();
$(this).parent().children(".entry-content").slideToggle(500); });
});

示例:http://jsfiddle.net/auUxk/

Just close them all up again every time one is clicked:

jQuery(document).ready(function($) {
$(".entry-content").hide('slow');
$(".entry-title").click(function() {
    $(".entry-content").hide();
$(this).parent().children(".entry-content").slideToggle(500); });
});

Example: http://jsfiddle.net/auUxk/

沧桑㈠ 2024-11-11 04:32:57

是的,您可以使用这个逻辑:

$(".entry-title").click(function() {
    // hide all entry title elements
    $('.entry-title').hide();
    // show the clicked one
    $(this).show();
});

您可以调整它以使用切换而不是隐藏/显示。

我通常通过为元素分配特殊的 css 类来实现这种逻辑(最好将事物分开)。因此,当我单击元素时,我会向其添加一个类。但是,当您单击另一个元素时,您必须使用该类的元素,但您只需要一个。所以你必须修改它。首先从所有元素中删除该类,然后将其添加到单击的元素中。

$(".anyAffectedElement").click(function() {
    // remove any instances of special class
    $('.enabledElement').removeClass('eneabledElement');
    // add special class to clicked element 
    $(this).addClass('enabledElement');
});

^ 这只是一个一般逻辑,你怎么能处理这样的事情。在简单的情况下,“enabledElement”类只是添加一些特殊的样式(如突出显示)。

Yes, you can just use this logic:

$(".entry-title").click(function() {
    // hide all entry title elements
    $('.entry-title').hide();
    // show the clicked one
    $(this).show();
});

You can adjust it to use toggle instead of hide/show.

I am usually doing this kind of logic by assigning special css classes to elements (its better to have things separated). So when I click element, I add a class to it. But then, when you click another element, you have to elements with that class, but you want only one. So you have to modify it. First remove that class from all elements with it, and then add it to clicked element.

$(".anyAffectedElement").click(function() {
    // remove any instances of special class
    $('.enabledElement').removeClass('eneabledElement');
    // add special class to clicked element 
    $(this).addClass('enabledElement');
});

^ This is just a general logic how can you deal with things like this. In simple cases, that 'enabledElement' class just adds some special styling (like highlight).

铃予 2024-11-11 04:32:57

我不明白其他人给出的那些例子,但最后我在另一个地方找到了答案,如果有人需要更简单(或不同)的解决方案:http://jsfiddle.net/bipen/zBdFQ/1/

@markratledge - 首先你需要提供你的 div 的 id 而不是类......

$("#link1").click(function(e) {
  e.preventDefault();
  $("#div1").slideDown(slow);
  $("#div2").slideUp(slow);
});

$("#link2").click(function(e) {
  e.preventDefault();
  $("#div1").slideUp(slow);
  $("#div2").slideDown(slow);
});

I did not understand those examples which were given by others, but finally i have found the answer in another place, if anyone needs a solution simplier (or different) : http://jsfiddle.net/bipen/zBdFQ/1/

@markratledge - first you need to give id of your divs instead of classes...

$("#link1").click(function(e) {
  e.preventDefault();
  $("#div1").slideDown(slow);
  $("#div2").slideUp(slow);
});

$("#link2").click(function(e) {
  e.preventDefault();
  $("#div1").slideUp(slow);
  $("#div2").slideDown(slow);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文