2013-11-01 148 views
0

我有一个克隆的元素,我想要它,所以如果我添加一个类到其中一个它检查主动删除是,并将其添加到此并转换为其他。这里就是我的工作:将类添加到多个元素

$(document).ready(function() { 


    $("li").click(function(){ 

     /*Here I want to add something like var active = $(.clonedelement + this, this) 
     but that does probably not makes sense, so what should i do? */ 

     var active = $(this) 

     // If this isn't already active 
     if (!$(this).hasClass("active")) { 
     // Remove the class from anything that is active 
     $("li.active").removeClass("active"); 
     // And make this active 
     active.addClass("active"); 

     } 
    }); 


}); 

眼下,它消除了当前的活动从两个,不也只添加类之一。

我做了它的jsfiddle http://jsfiddle.net/pintu31/8BxuE/

function UpdateTableHeaders() { 
    $(".persist-area").each(function() { 

     var el    = $(this), 
      offset   = el.offset(), 
      scrollTop  = $(window).scrollTop(), 
      Header = $("#headerny", this) 

     if ((scrollTop > offset.top) && (scrollTop < offset.top + el.height())) { 
      Header.addClass("floatingHeader"); 
     } else { 
      Header.removeClass("floatingHeader"); 

     }; 
    }); 
} 
// DOM Ready  
$(function() { 

    $(window) 
    .scroll(UpdateTableHeaders) 
    .trigger("scroll"); 

}); 

回答

1

试试这个

demo updated 1
demo updated 2 //与克隆(真)
demo updated 3 //与克隆(假) - 默认
demo updated 4

$(document).ready(function() { 
    $(document).on('click', 'li', function(){ 
     var ind = $(this).index(); 
     $('li').removeClass('active'); 
     $('li').eq(ind).addClass('active'); 
     $('#header1').empty(); 
     $('#header').find('ul').clone(true).appendTo('#header1'); 
    }); 
}); 
+0

它仍然不能将它添加到其他的元素之一;现场看到主要的导航这里的http:// richardhedberg .com/portfoliony/index.html –

+0

@RichardHedberg,检查更新后的代码是否需要th是什么? –

+0

谢谢,但不是真的我在找什么。我会试着解释一下,我有一个带有li的标题,当向下滚动时它会复制并隐藏第一个。我需要活动状态在他们之间转移。我会将克隆脚本添加到初始文章中,以便您自己查看。 –

1

如果你只需要突出与一流的活跃点击的元素,并删除所有其他人那么试试这个:

$("li").click(function(){ 
    $("li").removeClass("active"); 
    $(this).addClass("active"); 
    }); 

你并不真的需要检查如果任这一点,或其他公司已经有了类,只需压路机为“主动”类了所有的人,并把它添加到一个已经点击

+0

它仍然没有将其添加到其他元素;现场看到主导航在它克隆它后,它击中顶部richardhedberg.com/portfoliony/index.html –

0
$(document).ready(function() { 


    $("li").click(function(){ 
     $("li").removeClass("active"); 
     // And make this active 
     $(this).addClass("active"); 

     } 
    }); 


}); 
+0

它仍然没有添加到另一个元素;如果你想添加到另一个元素,它可以实现:$(“li”)。点击主导航到它后克隆它,然后点击顶部richardhedberg.com/portfoliony/index.html –

+0

(函数(){$ ( “李”)addClass( “活动”); // 使这个活动的 $(本).removeClass( “活动”); } }); – afei

+0

我想最后有一个支架太多了?这几乎就在那里,但是,除了当前显示的那个元素之外,它对这两个元素中的所有li项都是活动的。 –