Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> jak zapisać krócej funkcję w jquery?
marcus755
post
Post #1





Grupa: Zarejestrowani
Postów: 158
Pomógł: 1
Dołączył: 6.12.2012

Ostrzeżenie: (0%)
-----


  1. $('#a').hover(function(e){
  2. e.preventDefault();
  3. $('#content-menu-dic ul li a#a').addClass('a1');
  4. $('#content-menu-dic ul li a#b').removeClass('a1');
  5. $('#content-menu-dic ul li a#c').removeClass('a1');
  6. });
  7. $('#b').hover(function(e){
  8. e.preventDefault();
  9. $('#content-menu-dic ul li a#a').removeClass('a1');
  10. $('#content-menu-dic ul li a#b').addClass('a1');
  11. $('#content-menu-dic ul li a#c').removeClass('a1');
  12. });
  13. $('#c').hover(function(e){
  14. e.preventDefault();
  15. $('#content-menu-dic ul li a#a').removeClass('a1');
  16. $('#content-menu-dic ul li a#b').removeClass('a1');
  17. $('#content-menu-dic ul li a#c').addClass('a1');
  18. });
Go to the top of the page
+Quote Post
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 561
Pomógł: 6315
Dołączył: 27.12.2004




Kod
$('#a, #b, #c').hover(function(e){
        e.preventDefault();
        
        $('#content-menu-dic ul li a#a').removeClass('a1');
        $('#content-menu-dic ul li a#b').removeClass('a1');
        $('#content-menu-dic ul li a#c').removeClass('a1');
//$('#content-menu-dic ul li a#'+this.id).addClass('a1');
jQuery(this).addClass('a1');
    });
Powód edycji: [nospor]:
Go to the top of the page
+Quote Post
b4rt3kk
post
Post #3





Grupa: Zarejestrowani
Postów: 1 933
Pomógł: 460
Dołączył: 2.04.2010
Skąd: Lublin

Ostrzeżenie: (0%)
-----


Prościej id a, b, c, itd. zastąpić nazwą klasą i wtedy wystarczy:

  1. $('.class').hover(function(e){
  2. e.preventDefault();
  3. $('.content-menu-dic ul li').removeClass('a1');
  4. $(this).find('.content-menu-dic ul li').addClass('a1');
  5. });


Z kodem HTML byłoby prościej, bo tak to nie wiem jakie masz zależności.
Go to the top of the page
+Quote Post
marcus755
post
Post #4





Grupa: Zarejestrowani
Postów: 158
Pomógł: 1
Dołączył: 6.12.2012

Ostrzeżenie: (0%)
-----


Dzięki:-)
Jesteście Wielcy:-)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.12.2025 - 17:50