1

我需要你的帮助。

因为我的数据的结构方式。我需要创建某种函数,允许用户将第 1 列中的记录号输入到输入框“记录”中。然后单击按钮,代码将从我的数据表中的第 1 列中找到完全匹配的值,并使用现有代码选择行以突出显示它。我似乎无法将某种逻辑放在一起如何完成这项工作。

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#data tr.normal td {
    color: #235A81;
    background-color: white;
}
#data tr.highlighted td {
    color: #FFFFFF;
    background-color: #235A81;
}




</style>
<script type='text/javascript'>
function test() {

var table = document.getElementById("data");
var thead = table.getElementsByTagName("thead")[0];
var tbody = table.getElementsByTagName("tbody")[0];
var ishigh


tbody.onclick = function (e) {
  e = e || window.event;
  var td = e.target || e.srcElement
  var row = td.parentNode;

  if (ishigh&&ishigh!=row){
    ishigh.className='';
  }
  row.className = row.className==="highlighted" ? "" : "highlighted";
  ishigh=row;
}







document.onkeydown = function(e){
    e = e || event;
    var code = e.keyCode, rowslim = table.rows.length - 2, newhigh;
    if(code === 38){ //up arraow
        newhigh = rowindex(ishigh) - 2;
        if(!ishigh || newhigh < 0){return GoTo('data', rowslim);}
        return GoTo('data', newhigh);
    } else if (code === 40){ //down arrow
        newhigh = rowindex(ishigh);
        if(!ishigh || newhigh > rowslim){return GoTo('data', 0);}
        return GoTo('data', newhigh);
    }
}

function GoTo(id,nu){
  var obj=document.getElementById(id),
      trs=obj.getElementsByTagName('TR');
  nu = nu + 1;
  if (trs[nu]){
    if (ishigh&&ishigh!=trs[nu]){
      ishigh.className='';
    }
    trs[nu].className = trs[nu].className=="highlighted" ? "" : "highlighted";
    ishigh=trs[nu];
   }
}

function rowindex(row){
    var rows = table.rows, i = rows.length;
    while(--i > -1){
        if(rows[i] === row){return i;}
    }
}


}//end of nested function

</script>


</head>
<body onload="test()">
  <table style="cursor: default;" id="data" cellspacing="1" border="1">
    <thead>
        <tr>
            <th>Record #</th>
            <th>first name</th>
            <th>last name</th>
            <th>age</th>
            <th>total</th>
            <th>discount</th>
            <th>diff</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>5</td>
            <td>peter</td>
            <td>parker</td>
            <td>28</td>
            <td>9.99</td>
            <td>20.3%</td>
            <td>+3</td>
        </tr>
        <tr>
            <td>3</td>
            <td>john</td>
            <td>hood</td>
            <td>33</td>
            <td>19.99</td>
            <td>25.1%</td>
            <td>-7</td>
        </tr>
        <tr>
            <td>1</td>
            <td>clark</td>
            <td>kent</td>
            <td>18</td>
            <td>15.89</td>
            <td>44.2%</td>
            <td>-15</td>
        </tr>
        <tr>
            <td>4</td>
            <td>bruce</td>
            <td>almighty</td>
            <td>45</td>
            <td>153.19</td>
            <td>44%</td>
            <td>+19</td>
        </tr>
        <tr>
            <td>2</td>
            <td>benjamin</td>
            <td>evans</td>
            <td>56</td>
            <td>153.19</td>
            <td>23%</td>
            <td>+9</td>
        </tr>
    </tbody>
</table>
<br>

Example: type the record's number in the input box and then select it:
<input type="text" id="record" />

</body>


</html>
4

2 回答 2

1

我仍然无法全面了解您正在尝试做的事情,但我编写了一个函数来获取第一个 td 包含记录的 tr 。

function findTrForRecord(record) {
    var nodelist = document.getElementById('data').getElementsByTagName('tr');
    for (var i = 0; i < nodelist.length; i++) {
        var tr = nodelist.item(i);
        var tds = tr.getElementsByTagName('td');
        if (tds.length > 0 && tds[0].innerHTML.trim() == record)
            return tr;
    }
}

从那里,您可以突出显示它或做任何您想做的事情。

/**
 * unhighlights previous row and highlights new row
 * that matches the record
 */
button.onclick = function () {
    var trs = document.getElementsByTagName('tr');
    for (var i = 0; i < trs.length; i++)
        if (trs[i].className.indexOf('highlighted') > 0) {
            trs[i].className.replace('highlighted','');
            break;
        }
    var record = document.getElementById('record').value;
    var tr = findTrForRecord(record);
    tr.className += ' highlighted';
}
于 2013-08-07T21:45:04.063 回答
0

我编辑了您提供的 html 文件。看一下这个。http://pastebin.com/Wz0EdBMV

于 2013-08-07T22:32:30.593 回答