2014-03-24 3 views
-2

요일을 목록에 표시하고 싶습니다. 오늘의 날에 나는 Today라는 단어를 보여주고 싶다. 자바 스크립트로 어떻게 할 수 있습니까? 물론요일 표시 자바 스크립트

// Find the unordered list element 
var list = $('ul.week-days'); 

// Check what day it is today (0..6) 
var today = (new Date()).getDay(); 

// Our day strings; in JS, "Sunday" is the beginning of the week 
var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; 

// Add a list item to the list, substituting "Today" if necessary 
$.each(days, function(i, v) { 
    list.append($('<li>' + (i == today ? 'Today' : v) + '</li>')); 
}); 

이는 <a><span>...</span></a> 구조를 실종하지만 난 당신을 위해 운동으로 떠날 것이다 :

<ul class="week-days"> 
    <li><a href="#"><span id="firstD">Today</span></a></li> 
    <li><a href="#"><span id="secondD">Tue</span></a></li> 
    <li><a href="#"><span id="thirdD">Wed</span></a></li> 
    <li><a href="#"><span id="forthD">Thu</span></a></li> 
    <li><a href="#"><span id="fifthD">Fri</span></a></li> 
    <li><a href="#"><span id="sixthD">Sat</span></a></li> 
    <li><a href="#"><span id="seventhD">Sun</span></a></li> 
</ul> 
+4

해결하려는 문제를 이해할 수 없습니다. JavaScript에 관한 질문을 올렸지 만 게시 한 코드는 마크 업뿐입니다. 질문을 명확히 할 수 있습니까? –

+0

http://jsfiddle.net/arunpjohny/C6LKS/1/ –

+0

감사합니다. @Arun. 당신의 대답은 최고의 것이 었습니다. – user3456282

답변

1

당신은 당신이로 유지하는 경우에만 페이지 하단에

<script type="text/javascript"> 
    var i = 1; 
    $('.week-days li').each(function(){ 
     if ((new Date()).getDay() == i) { 
     $(this).val('Today').html('Today'); 
     } 
     i++; 
    }); 
</script> 
0

당신이 (같은 것을 할 수있는 (바로 위의 몸 닫는 태그)이 추가하여이 작업을 수행 할 수 있습니다 3 글자 abbr)

var date = new Date().toString(); // Get the Date and return it as a string 
var day = date.split(' ')[0]; // Split the date and store the first index (day) 
$('.week-days li').each(function(){ // loop over the li 
    if($(this).html() === day){ // if this html is the same as day variable 
     $(this).html('Today'); // change the html to 'Today' 
    } 
});