admin管理员组文章数量:1344322
HTML
<table id="table">
<tr>
<td>1999.09-2007.06</td>
<td> Secondary School </td>
</tr>
<tr>
<td>2007.09-2011.06</td>
<td> College </td>
</tr>
<tr>
<td>2011.09-2015.06</td>
<td> University</td>
</tr>
</table>
<form autoplete="on">
<p/>
<label>Year</label>
<input type="text" id="year" name="year" autofocus/>
<p/>
<label>Month</label>
<input type="text" name="month" id="month" />
<p/>
</form>
<input type="button" onClick="method()" />
JavaScript
function method(){
var year = document.getElementById("year").value;
var month = document.getElementById("month").value;
var data = new Date(year,month-1,1);
var dataTable = document.getElementById("table");
var cells = dataTable.querySelectorAll("td");
var cells2 = dataTable.querySelectorAll("td+td");
for (var i = 0; i < cells.length; i++){
var mikro = cells[i].firstChild.data.split("-");
var place = cells2[i].firstChild.data;
var firstData = mikro[0].split(".");
var secondData = mikro[1].split(".");
var data1 = new Date(firstData[0],firstData[1]-1,1);
var data2 = new Date(secondData[0],secondData[1]-1,1);
if(data1<=data && data2>=data){
alert(place);
}
}
}
Here i have 2 columns in the table, i want to create script which will help me to find information from it: I have <input type="text" id="year" name="year" autofocus/>
and <input type="text" name="month" id="month" />
where i can enter year & month.After entering year&month i have to get information from the 2nd column according to the dates on the 1st column for example: " Input year: 2002, Input month:05 - result must be 'Secondary School'" because it between 1999.09-2007.06 . I can't see what is wrong with my script, it always give me false information.
HTML
<table id="table">
<tr>
<td>1999.09-2007.06</td>
<td> Secondary School </td>
</tr>
<tr>
<td>2007.09-2011.06</td>
<td> College </td>
</tr>
<tr>
<td>2011.09-2015.06</td>
<td> University</td>
</tr>
</table>
<form autoplete="on">
<p/>
<label>Year</label>
<input type="text" id="year" name="year" autofocus/>
<p/>
<label>Month</label>
<input type="text" name="month" id="month" />
<p/>
</form>
<input type="button" onClick="method()" />
JavaScript
function method(){
var year = document.getElementById("year").value;
var month = document.getElementById("month").value;
var data = new Date(year,month-1,1);
var dataTable = document.getElementById("table");
var cells = dataTable.querySelectorAll("td");
var cells2 = dataTable.querySelectorAll("td+td");
for (var i = 0; i < cells.length; i++){
var mikro = cells[i].firstChild.data.split("-");
var place = cells2[i].firstChild.data;
var firstData = mikro[0].split(".");
var secondData = mikro[1].split(".");
var data1 = new Date(firstData[0],firstData[1]-1,1);
var data2 = new Date(secondData[0],secondData[1]-1,1);
if(data1<=data && data2>=data){
alert(place);
}
}
}
Here i have 2 columns in the table, i want to create script which will help me to find information from it: I have <input type="text" id="year" name="year" autofocus/>
and <input type="text" name="month" id="month" />
where i can enter year & month.After entering year&month i have to get information from the 2nd column according to the dates on the 1st column for example: " Input year: 2002, Input month:05 - result must be 'Secondary School'" because it between 1999.09-2007.06 . I can't see what is wrong with my script, it always give me false information.
1 Answer
Reset to default 7Try this (jsfiddle), increment the cell index by 2 each time, and use the simpler td
selector:
function method () {
var year = document.getElementById("year").value;
var month = document.getElementById("month").value;
var data = new Date(year, month - 1, 1);
var dataTable = document.getElementById("table");
var cells = dataTable.querySelectorAll("td");
for (var i = 0; i < cells.length; i+=2) {
var mikro = cells[i].firstChild.data.split("-");
var place = cells[i+1].firstChild.data;
console.log(mikro, place);
var yearMonth1 = mikro[0].split(".");
var yearMonth2 = mikro[1].split(".");
var data1 = new Date(yearMonth1[0], yearMonth1[1] - 1, 1);
var data2 = new Date(yearMonth2[0], yearMonth2[1] - 1, 1);
if (data1 <= data && data2 >= data) {
alert(place);
}
}
}
本文标签: JavaScript HTML table parsingStack Overflow
版权声明:本文标题:JavaScript HTML table parsing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743763224a2534766.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论