我目前正在尝试为我的网站选择一天,但我遇到了一些问题。我目前的问题就在于此。
底部的if语句似乎没有搜索并选择包含该数据的数据?我使用javascript并不是很好,所以我不太确定你是否会这样做?
总体而言,我希望它能够选择名称为 show 的所有类,然后搜索其中的内容以查看它是否等于今天的日期,如果是,则会为其着色绿色。我已经尝试过不同的方式来做这件事,但我似乎无法理解它并且我也在这里看看是否有人试图做某事同样但他们的所有日期选择,而不是日期选择。
使用Javascript:
<script>
function showDate()
{
var now = new Date();
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var shows = document.getElementsByClassName('show');
var today = shows.innerHTML;
var showElements = document.querySelectorAll('show');
document.getElementById('dateToday').innerHTML = 'Today is: ' + dayNames[now.getDay()];
if(today.contains(dayNames[now.getDay()]))
{
showElements.style.color = '#00E42D';
}
}
</script>
HTML:
<ul class="active">
<h1>Active Shows</h1>
<li id="SOA" class="show"><strong>Sons of Anarchy</strong> - Tuesday</small></li>
<li id="SP" class="show"><strong>Southpark</strong> - Wednesday</li>
<li id="WLR" class="show"><strong>Waterloo Road</strong> - Wednesday</li>
<li id="TWD" class="show"><strong>The Walking Dead</strong> - Sunday</li>
<li id="FG" class="show"><strong>Family Guy</strong> - Sunday</li>
</ul>
答案 0 :(得分:0)
易于使用的jQuery
这是js代码:
<script>
function showDate() {
var now = new Date();
var dayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var today = dayNames[now.getDay()];
$('dateToday').html( 'Today is: ' + today);
$(".show:contains("+today+")").css("color","#00E42D");
}
</script>