jQuery:如何“取消点击”一个打开的slideToggle div?

发布于 2024-11-04 10:25:49 字数 929 浏览 0 评论 0原文

是否可以“取消点击”已使用 SlideToggle 打开的活动 div?

此函数打开多个彼此独立的文本 div,但打开的 div 的 click(function() 仍然可点击,因此 div 会弹起关闭,然后再次打开。这主要是一种用户体验;我希望用户无法单击已打开的 div

jsfiddle: http:// /jsfiddle.net/auUxk/12/

$(".entry-content").hide();
$(".entry-title").click(function() {
    $(".entry-content").hide();
$(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 Lorem Ipsum

    </div></div>

<div class="entry-post">

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

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

    </div></div>

Is it possible to "declick" an active div that has been opened with slideToggle?

This function opens multiple text divs independently from each other, but the open div's click(function() is still clickable, and so the div will bounce closed and then open again. It's mostly a user experience; I'd like users to be unable to click an already opened div.

jsfiddle: http://jsfiddle.net/auUxk/12/

$(".entry-content").hide();
$(".entry-title").click(function() {
    $(".entry-content").hide();
$(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 Lorem Ipsum

    </div></div>

<div class="entry-post">

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

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

    </div></div>

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

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

发布评论

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

评论(3

天生の放荡 2024-11-11 10:25:49

我想这就是你想要的。似乎大多数人都没有注意到您一次只想保留一个打开的 div。

http://jsfiddle.net/entropo/WnpGv/

jQuery(document).ready(function($) {
    $(".entry-title").click(function() {
        $this = $(this);
        var $content = $this.next(".entry-content");
        if (! $content.is(":visible")) {
            $(".entry-content:visible").slideToggle(100);
            $content.slideToggle(500);
        }
    });
});

编辑:< br>
这种 UI 模式称为“手风琴”。还有很多插件可以以更戏剧性/复杂的方式实现这一点方式。

另外, jQuery UI 有一个手风琴 在他们的 API 中。请记住,手风琴 API 是正在针对 1.9 进行修改

I think this is what you want. It seems most people didn't notice that you want to keep only one open div at a time.

http://jsfiddle.net/entropo/WnpGv/

jQuery(document).ready(function($) {
    $(".entry-title").click(function() {
        $this = $(this);
        var $content = $this.next(".entry-content");
        if (! $content.is(":visible")) {
            $(".entry-content:visible").slideToggle(100);
            $content.slideToggle(500);
        }
    });
});

Edit:
This UI pattern is called an accordion. There are also lots of plugins which achieve this in more dramatic/complicated ways.

Also, jQuery UI has an accordion in their API. Keep in mind that the accordion API is being revamped for 1.9.

肩上的翅膀 2024-11-11 10:25:49

“我希望用户无法点击
一个已经打开的 div。”

这将起作用:http://jsfiddle.net/xixionia/9P6My/< /a>

<div>
    <h2 class="entry-title">Post Title 1</h2>
    <div class="entry-content">Lorem Ipsum Lorem Ipsum</div>
</div>

<div>
    <h2 class="entry-title">Post Title 1</h2>
    <div class="entry-content">Lorem Ipsum Lorem Ipsum</div>
</div>

$(".entry-title").click(function() {
    $(this)
        .next('.entry-content:not(:visible)')
        .slideToggle(500);
});

CSS:

.entry-content
{
    display: none;
}

或者,您始终可以跳过 css 并在所有条目内容分类元素上使用 hide() :)

这将切换带有“.entry”的第一个后续元素 。 -内容”类不可见,并且不会将它们滑动关闭

如果您想完全解除自己与点击事件的绑定,您可以使用 undbind('click')

$(".entry-title").bind('click', function() {
    $(this)
        .unbind('click', arguments.callee)
        .next('.entry-content')
        .slideToggle(500);
});

这将是最明智的方法,并且不会取消绑定可能绑定到点击事件的其他函数

或者,您可以使用事件命名空间取消绑定:

$(".entry-title").bind('click.slideopen', function() { // bind a click event with the slideopen namespace
    $(this)
        .unbind('.slideopen') // unbind all events in the slideopen namespace
        .next('.entry-content')
        .slideToggle(500);
});

您可以在此处查看一个示例:http://jsfiddle.net/xixionia/9P6My/6/

"I'd like users to be unable to click
an already opened div."

This will work: http://jsfiddle.net/xixionia/9P6My/

<div>
    <h2 class="entry-title">Post Title 1</h2>
    <div class="entry-content">Lorem Ipsum Lorem Ipsum</div>
</div>

<div>
    <h2 class="entry-title">Post Title 1</h2>
    <div class="entry-content">Lorem Ipsum Lorem Ipsum</div>
</div>

and

$(".entry-title").click(function() {
    $(this)
        .next('.entry-content:not(:visible)')
        .slideToggle(500);
});

And the CSS:

.entry-content
{
    display: none;
}

Or, you can always skip the css and use hide() on all of your entry-content classed elements. :)

This will toggle slide the first following element with the ".entry-content" class that are not visible, and will not slide them closed.

If you would like to actually unbind yourself from the click event entirely, you could use undbind('click')

$(".entry-title").bind('click', function() {
    $(this)
        .unbind('click', arguments.callee)
        .next('.entry-content')
        .slideToggle(500);
});

This would be the most savvy approach, and will not unbind your other functions which may be bound to the click event.

Or, you could unbind using event namespaces:

$(".entry-title").bind('click.slideopen', function() { // bind a click event with the slideopen namespace
    $(this)
        .unbind('.slideopen') // unbind all events in the slideopen namespace
        .next('.entry-content')
        .slideToggle(500);
});

You can see an example of this here: http://jsfiddle.net/xixionia/9P6My/6/

对不⑦ 2024-11-11 10:25:49

如果你想点击关闭,你可以这样做:

$(".entry-content").hide();
$(".entry-title").click(function() {
    $(".entry-content").hide();
    $(this).parent().children(".entry-content:visible").slideToggle(500); 
});

这将关闭所有div,但不会再次打开它。如果你想保持打开状态:

$(".entry-content").hide();
$(".entry-title").click(function() {
    var d = $(this).next();
    if (!d.is(":visible")) {
        $(".entry-content").hide();
        d.slideToggle(500); 
    }
});

If you want to click on close, you can do this:

$(".entry-content").hide();
$(".entry-title").click(function() {
    $(".entry-content").hide();
    $(this).parent().children(".entry-content:visible").slideToggle(500); 
});

This will close all divs, but not open it again. If you want to keep it open:

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