开发者

highlighting variable date in a table

Good morning,

I am having trouble with the following code

<html>
<head>
<title>highlight date</title>
<style>
.row {
background-color: Yellow;
color:blue;
}

</style>
<script type="text/javascript">

</script>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script language="javascript">
var currentTime = new Date();
var day = currentTime.getDate();
v开发者_运维知识库ar month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
var review = day + "/" + month + "/" + year;

</script>
<script language="javascript">
$(document).ready(function() {
$('#names td:contains(xxxx)').parent().addClass('row');
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="names">
<tbody>
<tr>
<td>1</td>
<td>review</td>
</tr>
<tr>
<td>2</td>
<td>Satheesh</td>
</tr>
<tr>
<td>3</td>
<td>08/10/2010</td>
</tr>
<tr>
<td>4</td>
<td>11/10/2010</td>
</tr>
<tr>
<td>5</td>
<td>xyz</td>
</tr>
</tbody>
</table>
</div>
</form>
<h4>It was
<script type="text/javascript">document.write(review)</script></h4>
</body>
</html>

I get get it to highlight a set string, but for some reason when ever i try to declare the variable "review" as what i want to search for and highlight, all it seems to find is the word review. can anyone help me on this at all please? its starting to do my head in.

Thankyou


You have not provided enough information.

My psychic debugging skills tell me that you need to change it to

$('#names td:contains(' + someVariable + ')')


I'm guessing you're trying

$('#names td:contains(review)').parent().addClass('row');

you should instead concatenate your variable:

$('#names td:contains(' + review + ')').parent().addClass('row');

which will join '#names td:contains(' to the value of your variable, followed by ')'

EDIT: I took pity. Below is my solution to the additional question you asked in the comments. Please note you should make the parseDate function more robust.

$(document).ready(function() {
    function parseDate(dateString)
    {
        //You should address this function to be more robust
        return new Date(Date.parse(dateString));
    }

    $('#names tr').each(function(index)
    {
        var row = $(this);
        if (parseDate(elem.find("td:eq(1)").text()) < new Date())
            row.addClass('row');
    });
});

This gets each row and iterates through them. It gets the textual value of the SECOND (0-index) td, and parses it to a date. If this date is less than today, it applies the class.

It DOES NOT check whether the data is an actual date or not, so you should add in that check somehow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜