简体   繁体   English

突出显示包含日期的div

[英]Highlight div that contains date

I'm trying to make code that changes the color of the div (with the class "carrythatweight") if it has the date in it. 我正在尝试制作可更改div(带有“ carrythatweight”类)颜色的代码(如果其中包含日期)。

This is the HTML I'm using: 这是我正在使用的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>​

This is the script I'm using, but it doesn't really work: 这是我正在使用的脚本,但实际上并不起作用:

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");
});​

Here is the Fiddle 这是小提琴

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

I made two fixes in your code to make it work: 我对您的代码进行了两个修复,以使其正常运行:

1) I replaced some single quotes with double quotes so that the selector gets interpolated as expected: Compare (original) 1)我用双引号替换了一些单引号,以便选择器按预期进行插值:比较(原始)

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

with (fixed) 与(固定)

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

2) I modified the class in the selector to match the class in the div s. 2)我修改了选择器中的类以匹配div中的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM