English 中文(简体)
页: 1 图瓦卢
原标题:jQuery toggle if NOT this

Forgive me if this is a newbie question:

I ve got a menu set to open the next div to see more details, and if there is another div open already it will close it while it opens the new one. I cannot figure out how to close the div already open if you click on the header associated with that div. It just closes & reopens.

Code:

$(document).ready(function() {

    $(".heading").click(function(){
        if($(".content").is(":visible")){
            $(".less").removeClass("less");
            $(".content").slideUp(500);
        }
        $(this).next(".content").slideToggle(500);
        $(this).children(".more").toggleClass("less");
    });

});

感谢任何帮助。

最佳回答

为此:

$(".heading").click(function(){
    // Cache stuff, so we don t have to keep generating jQuery objects
    var $this = $(this);
    var $thisContent = $this.next(".content");

    // Saves a boolean value to see if the clicked element s content is
    // currently visible
    var thisWasVisible = $thisContent.is(":visible");

    // Removes less class and slides up ALL visible content divs
    $(".less").removeClass("less");
    $(".content").slideUp(500);

    // Slide the clicked element s content div down and add less class,
    // but only if it s content was not originally visible. We don t want
    // to show it again if it was just hidden.
    if(!thisWasVisible){
        $thisContent.slideDown(500);
        $this.children(".more").addClass("less");
    }
});
问题回答

引言

$(document).ready(function() {

$(".heading").click(function(){
    if($(".content").is(":visible")){
        $(".less").removeClass("less");
        $(".content").slideUp(500);
    } else {
        $(this).next(".content").slideDown(500);
        $(this).children(".more").toggleClass("less");
    }
});

});

或许可以说,对你应该做的工作的侵略性变化最小。





相关问题
"this" keyword in IE

So, I using this snippet <tr onclick="this.toggleClassName( selected )"> on a web page. (a Prototype.js function) It works great. All I do is click on a table row, and it appears to get ...

Accessing an outer class from inside a listener?

I have a listener inside Class A, and I want to pass Class A to my Class B inside the listener. Normally I d just use this, but then I d get the event that triggered the listener.

this, current context-when should I use in jQuery?

I am not very sure with the use of "this" [current context] in jquery.What I know is- it prevents the dom from searching all the elements, it just work on that current element, which improve ...

Why is this not updating to refer to a new object?

I m writing an online game which allows a user to progress from one puzzle to the next, and if the user makes mistakes, each puzzle has a start again button to allow the user to start just that puzzle ...

Should the RequireThis check in Checkstyle be enabled?

One of the built-in Checkstyle checks is RequireThis, which will go off whenever you don t prepend this. to local field or method invocations. For example, public final class ExampleClass { ...

热门标签