单击a 时,我正在尝试对 DOM 应用更改<tr>
。单击时,它应该添加data-state=enabled
和data-display=expanded
到单击的<tr>
同时应用data-state=disabled
和data-display=collapsed
到其他所有 <tr>
。
因此,它应该看起来像突出显示单击的行,同时禁用其他行。
然后,当一行被突出显示,并且用户点击其他地方时,它应该重置为 default,即data-state=enabled
and data-display=collapsed
for all<tr>
目前,我让它工作,以便当单击 a 时<tr>
,该行被突出显示并且所有其他行被禁用。但是,该脚本缺少重置为默认值,因为如果<tr>
单击另一个,它会立即突出显示那个并禁用其余部分。
我想在 vanilla javascript 中执行此操作,但如果它更容易并且不会显着影响性能,我也愿意使用 jQuery。
这是带有工作代码的 JSbin,可以查看它的位置: https ://jsbin.com/kirati/
到目前为止的代码:
<table class="table">
<thead>
<tr>
<th class="sort-key asc"><a href="#">Pet Name </a></th>
<th><a href="#">Owner (Last, First)</a></th>
<th><a href="#">Species</a></th>
<th><a href="#">Breed</a></th>
<th><a href="#">ID</a></th>
</tr>
</thead>
<tbody>
<tr>
<td>
Fluffy
<div class="table-row__expanded-content">
<data-key>Sex: </data-key> <data-value>Male</data-value><br />
<data-key>DOB: </data-key> <data-value>12/08/2010</data-value> <br />
<data-key>Weight: </data-key> <data-value>20 lbs</data-value> <br />
<data-key>Location: </data-key> <data-value>Kennel 2B</data-value> <br />
<data-key>Temperament: </data-key> <data-value>Aggresive</data-value> <br />
<data-key>Allergies: </data-key> <data-value>Sulfa, Penicillin, Peanuts</data-value>
</div>
</td>
<td>Anderson, James</td>
<td>Dog</td>
<td>Bulldog-Shitzu</td>
<td>ABCDE1234567</td>
</tr>
<tr>
<td>Feather</td>
<td>Michelle Charlotte, <br /> Angel Vanessa</td>
<td>Cat</td>
<td>American Bobtail</td>
<td>FGHIJ1234567</td>
</tr>
<tr>
<td>Fluffer-Nutter</td>
<td>Rakerstraw, Rickey</td>
<td>Dog</td>
<td>American Eskimo</td>
<td>KLMNO1234567</td>
</tr>
<tr>
<td>Farley</td>
<td>Cunningham, Stephanie</td>
<td>Dog</td>
<td>Pomeranian</td>
<td>PQRST1234567</td>
</tr>
<tr>
<td>Fuzzy</td>
<td>Venice, Harding</td>
<td>Cat</td>
<td>Burmese</td>
<td>UVWXY1234567</td>
</tr>
<tr class="alphabet-label">
<td>G</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Goldie</td>
<td>Cherilyn, Mitchener</td>
<td>Dog</td>
<td>Chihuahua-Maltese</td>
<td>ZABCD1234567</td>
</tr>
</tbody>
</table>
和 Javascript
window.onload = function () {
var tablerow = document.body.getElementsByTagName('tr');
console.log(tablerow);
// Convert the HTMLCollection into a true javascript Array, so we can do "stuff" with it
var tablerowArr = Array.prototype.slice.call(tablerow);
console.log(tablerowArr);
// Do stuff
tablerowArr.forEach(function (value, i) {
console.log(i, value);
tablerow[i].onclick = function (e) {
//console.log("clicked!");
var newArr = tablerowArr.slice(i, i + 1);
//console.log(tablerow);
console.log(i);
//console.log(tablerowArr);
console.log('newArr', newArr);
tablerowArr.forEach(function (value, i) {
// first reset all instances of data-XXX
tablerowArr[i].setAttribute('data-display', "collapsed");
// tablerowArr[i].setAttribute('data-state', "enabled");
// Set the <tr> data-display attribute to expanded/collapsed on click
newArr[0].setAttribute('data-display', tablerowArr[i].getAttribute('data-display') === "collapsed" ? "expanded" : "collapsed");
//tablerowArr[i].setAttribute('data-display', tablerowArr[i].getAttribute('data-display') === "collapsed" ? "expanded" : "collapsed");
// Set the <tr> data-state attribute to enabled/disabled on click
newArr[0].setAttribute('data-state', newArr[0].getAttribute('data-state') === "disabled" ? "enabled" : "enabled");
tablerowArr[i].setAttribute('data-state', newArr[0].getAttribute('data-state') === "enabled" ? "disabled" : "enabled");
});
e.preventDefault();
};
});
};