突出显示包含日期的div

时间:2012-12-26 21:42:21

标签: javascript jquery

我正在尝试使用更改div颜色的代码(使用类“carrythatweight”),如果它有日期的话。

这是我正在使用的HTML:

<div class="carrythateright">groan...</div>
<div class="carrythateright">what in the world is going on !!!</div>
<div class="carrythateright">stackoverflow is SO helpful. It is now 12/26/2012 !!!!</div>​

这是我正在使用的脚本,但它并没有真正起作用:

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()

$(document).ready(function() {
    $(".carrythatweight:contains('' + month + '/' + day + '/' + year +'')").css("color", "#00bbbb");
});​

以下是Fiddle

1 个答案:

答案 0 :(得分:4)

http://jsfiddle.net/37tR5/2/

我在您的代码中进行了两处修复以使其正常工作:

1)我用双引号替换了一些单引号,以便选择器按预期进行插值:比较(原始)

"...'' + month + '/' + day + '/' + year +'')"

with(fixed)

"...'" + month + "/" + day + "/" + year +"')"

2)我在选择器中修改了类以匹配div中的类。