10
I would like to know how to find a certain element <tr>
in a numbered table. For example: In a table where each row <tr>
has its cells <td>
numbered 1-5, containing 4 lines <tr>
, if I pass number 15, I get as a result the 3rd line <tr>
, if I pass number 17, I get as a result the 4th line <tr>
, and so on.
With table.match(/<tr>/g)
, I get all 4 elements <tr>
, but what I want is to get just one element <tr>
, as a final result, according to the numerical criterion explained above.
The goal is to apply a style to the tag <tr>
, an outline, outline: solid 1px red;
, to highlight.
var table = ['<table>
<tr>
<td>1</td><td>2</td><td>3</td><td>4</td><td>5</td>
</tr>
<tr>
<td>6</td><td>7</td><td>8</td><td>9</td><td>10</td>
</tr>
<tr>
<td>11</td><td>12</td><td>13</td><td>14</td><td>15</td>
</tr>
<tr>
<td>16</td><td>17</td><td>18</td><td>19</td><td>20</td>
</tr>
</table>'];
table.match(/<tr>/g); // Me devolve todos os elementos <tr>
The table is originally the function below that generates a Calendar. I was able to highlight the present day. Now the challenge is do the same with the current week:
function Calendar(id, year, month) {
var elem = document.getElementById(id)
var mon = month - 1
var d = new Date(year, mon)
var e = new Date();
var weekDay = ["Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"];
var months = ['January', 'Frebruary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var table = ['<table><tr>']
table.push('<th colspan = 4>' + months[mon] + '</th>' + '<th colspan = 3>' + year + '</th>' + '</tr><tr>')
for (var i = 0; i <= 6; i++ ) {
table.push('<th>' + weekDay[i] + '</th>')
}
table.push('</tr><tr>')
for (var i=0; i<d.getDay(); i++) {
table.push('<td></td>')
}
while(d.getMonth() == mon) {
if (d.getDate() == e.getDate()) { // Condicional para dar destaque ao dia atual
table.push('<td class=day>'+d.getDate()+'</td>')
} else {
table.push('<td>'+d.getDate()+'</td>')
}
if (d.getDay() % 7 == 6) {
table.push('</tr><tr>')
}
d.setDate(d.getDate()+1)
}
for (var i=d.getDay(); i<7; i++) {
table.push('<td></td>')
}
table.push('</tr></table>')
elem.innerHTML = table.join('\n')
}
new Calendar("cal", 2015, 3);
This HTML does not have enough distinct marks for use of regex, I think a solution that parses the fragment will suit you better.
– Miguel Angelo