如何将类添加到已分配给它的类的现有li中?

时间:2013-08-16 18:58:19

标签: javascript css ajax jquery

如何将类添加到已分配给它的类的现有li

我当前的代码已开启 http://jsfiddle.net/GbPdV/

我想将“Open”类添加到:

< li class="dropdown hover carticon cart" >

我认为这必须发生在:(不确定)

 if ( response.Status == "Success" )
       {

            $.ajax({
                type: "GET",
                url: "dmiajax.aspx?request=FloatingCart&extra=" + rnd(),
                dataType: "html",
                success: function(response) {
                    var floatingCart = $("ul.dropdown-menu.topcart");

                    if (floatingCart.length == 0) {
                        floatingCart = $('<ul class="dropdown-menu topcartopen"></ul>').insertBefore(".pull-right");
                        floatingCart.hoverIntent({
                            over: function() {},
                            timeout: 200,
                            out: function() {
                                $(this).stop(true, true).filter(":visible").hide("drop", {
                                    direction: "down"
                                });
                            }
                        });

                    }

1 个答案:

答案 0 :(得分:6)

只需使用.addClass

floatingCart.addClass("Open");

addClass

success: function(response) {
    var floatingCart = $("ul.dropdown-menu.topcart");
        //HERE
        floatingCart.addClass("Open");
    if (floatingCart.length == 0) {
        //OR HERE
        floatingCart = $('<ul class="dropdown-menu topcartopen"></ul>')
               .insertBefore(".pull-right")
               .addClass("Open");;
        floatingCart.hoverIntent({
            over: function() {},
            timeout: 200,
            out: function() {
            $(this).stop(true, true).filter(":visible").hide("drop", {
            direction: "down"
        });
    }
}