Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> jak js krócej zapisać w jquery?
marcus755
post 12.08.2014, 11:25:19
Post #1





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

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


Znalazłem coś takiego w js:

  1. var my_data = new Date();
  2. var godz = my_data.getHours();
  3.  
  4. var IMG = new Array();
  5. IMG[0]=["noc.jpg", 6];
  6. IMG[1]=["rano.jpg", 12];
  7. IMG[2]=["poludnie.jpg", 16];
  8. IMG[3]=["wieczor.jpg", 19];
  9.  
  10. var str="";
  11. for (var i=1; i<IMG.length; i++)
  12. {
  13. if((godz>IMG[i-1][1])&&(godz<=IMG[i][1])) str=IMG[i][0];
  14. }
  15. if(str=="") str=IMG[0][0];
  16.  
  17. document.write("<img src="+str+">");


jak to krócej zapisać w jquery?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 7)
trueblue
post 12.08.2014, 13:44:12
Post #2





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


Zawsze możesz skrócić w czystym JS:
  1. var godz=new Date().getHours();
  2. var IMG=[["noc.jpg",6],["rano.jpg",12],["poludnie.jpg",16],["wieczor.jpg",19]];
  3. var str=IMG[0][0];
  4. for(var i=1;i<IMG.length;i++)
  5. if((godz>IMG[i-1][1])&&(godz<=IMG[i][1])) str=IMG[i][0];
  6. document.write("<img src="+str+">");


--------------------
Go to the top of the page
+Quote Post
marcus755
post 12.08.2014, 14:10:28
Post #3





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

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


ok dzięki:-) rozumiem,
a jak modyfikując powyższy kod osiągnąć następujący efekt, tzn. mam div'y, a w nich dynamicznie z CMS'a zaciągane godziny (jak poniżej),
jak z każdego div'a wyciągnąć godzinę i przyporządkować każdej godzinie inny obrazek? :
Obrazków jest 24, tyle ile ma doba, ale nie wszystkie są dodawane w CMS-ie...
...jednak te które się wyświetlają muszą być do nich prawidłowo przyporządkowane obrazki, jak to zrobić?

<div class="time">12:00</div>
<div class="time">14:00</div>
<div class="time">15:00</div>
<div class="time">19:00</div>
Go to the top of the page
+Quote Post
trueblue
post 12.08.2014, 14:12:12
Post #4





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


A gdzie ma być wstawiony obrazek przyporządkowany danej godzinie z diva?


--------------------
Go to the top of the page
+Quote Post
marcus755
post 12.08.2014, 14:45:33
Post #5





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

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


w miejsca wielokropków <img src=""> z przyporządkowanym do godziny

<div class="block">
<div class="time">12:00</div>
<div class="timer">...</div>
</div>
<div class="block">
<div class="time">14:00</div>
<div class="timer">...</div>
</div>
<div class="block">
<div class="time">16:00</div>
<div class="timer">...</div>
</div>
<div class="block">
<div class="time">18:00</div>
<div class="timer">...</div>
</div>

Ten post edytował marcus755 12.08.2014, 14:46:20
Go to the top of the page
+Quote Post
trueblue
post 12.08.2014, 14:50:46
Post #6





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


  1. var godz=new Date().getHours();
  2. var IMG=[["noc.jpg",6],["rano.jpg",12],["poludnie.jpg",16],["wieczor.jpg",19]];
  3. $('div.time').each(function(){
  4. var str=IMG[0][0];
  5. var godz=parseInt($(this).text()); //tylko dla pełnych godzin w divach
  6. for(var i=1;i<IMG.length;i++)
  7. if((godz>IMG[i-1][1])&&(godz<=IMG[i][1])) str=IMG[i][0];
  8. $(this).next().html("<img src="+str+">");
  9. });


--------------------
Go to the top of the page
+Quote Post
Crozin
post 12.08.2014, 14:53:13
Post #7





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Podstawowe pytanie: po co chcesz skracać i tak krótki kod?
Go to the top of the page
+Quote Post
marcus755
post 12.08.2014, 15:28:08
Post #8





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

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


Genialnie,
Wielkie Dzięki - Działa - The Beściaku;-)

a jak zrobić dla pólnocy?

http://jsfiddle.net/t4p54td5/3/

bo ani jak wpisuję 24 ani 0 nie działa....

Ten post edytował marcus755 12.08.2014, 15:07:36
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 04:21