[英]How to access element attribute whose display is none?
I am trying to access the attribute/value of 'schoolidx' that is not hidden, when add button is clicked; 单击添加按钮时,我试图访问未隐藏的“ schoolidx”的属性/值; however I am having hard time accessing it.
但是我很难访问它。
Can someone guide me please? 有人可以指导我吗? I am trying to get only "1", or "2", or "3" depending on which one is not hidden ("display: none;").
我试图只获得“ 1”,“ 2”或“ 3”,具体取决于哪一个未被隐藏(“ display:none;”)。 In this case, "1".
在这种情况下,为“ 1”。
Tried different methods: 尝试了不同的方法:
let schoolIndex = $('#schoolTableBody > tr:not([style*="display: none"])').attr("schoolidx"),
schoolIndex2 = $('tr').filter(function(){ return $(this).css("display") != "none";}).attr("schoolidx"),
schoolIndex3 = $('tr').filter(function(){ return $(this).css("display") != "none";}).attr("schoolidx"),
schoolIndex4 = $("#schoolTableBody > tr:visible")
<table class="table table-striped table-hover table-condensed" id="schoolTable">
<thead>
<tr>
<th> </th>
<th>School Name</th>
<th>Country</th>
</tr>
</thead>
<tbody id="schoolTableBody">
<tr class="school-row" style="">
<td><button id="addSchoolBtn" title="Add" style="width:50px"/></td>
<td><input type="text" name="sName" id="sName"></td>
<td><input type="text" name="Country" id="Country"></td>
</tr>
<tr schoolidx="1" style="">
<td> </td>
<td><input type="hidden" name="sName" value="SK">SK</td>
<td><input type="hidden" name="Country" value="US">US</td>
</tr>
<tr schoolidx="1" style="">
<td> </td>
<td><input type="hidden" name="sName" value="JS">JS</td>
<td><input type="hidden" name="Country" value="US">US</td>
</tr>
<tr schoolidx="2" style="display: none;">
<td> </td>
<td><input type="hidden" name="sName" value="CAS">CAS</td>
<td><input type="hidden" name="Country" value="CA">CA</td>
</tr>
<tr schoolidx="2" style="display: none;">
<td> </td>
<td><input type="hidden" name="sName" value="AM">AM</td>
<td><input type="hidden" name="Country" value="CA">CA</td>
</tr>
<tr schoolidx="3" style="display: none;">
<td> </td>
<td><input type="hidden" name="sName" value="BAS">BAS</td>
<td><input type="hidden" name="Country" value="BR">BR</td>
</tr>
</tbody>
</table>
Instead of attempting to search by styling, perhaps you could add an 'active' class when you unhide a given row, and remove it when you hide it again. 不必尝试通过样式搜索,也可以在取消隐藏给定行时添加“活动”类,并在再次隐藏该行时将其删除。
You can then search using jQuery. 然后,您可以使用jQuery搜索。
var schoolid = $("tr.active").prop("schoolidx");
Have a look at addClass and removeClass . 看看addClass和removeClass 。 I assume you already have the ability to hide each row.
我假设您已经可以隐藏每一行。 Just add those two functions to that bit of code.
只需将这两个函数添加到该代码段中即可。
Note that I have changed your 'attribute' to a valid data attribute 请注意,我已将您的“属性”更改为有效的数据属性
For the JS, we have to make sure we're only selecting the tr
's within the tbody
but not the first tr
as thats where the add button is. 对于JS,我们必须确保我们只选择了
tr
的范围内的tbody
首不见 tr
因为多数民众赞成在ADD按钮。
Then do an if()
within an onclick
to check to see if the display
is set to none
然后在
onclick
执行if()
以检查display
是否设置为none
<button>
aren't self closing, they require a </button>
<button>
不是自动关闭的,它们需要</button>
document.getElementById('addSchoolBtn').addEventListener('click', () => { document.querySelectorAll('#schoolTableBody tr:not(.school-row)').forEach(tr => { if (tr.style.display !== "none") alert(tr.dataset.schoolidx) }) })
<table class="table table-striped table-hover table-condensed" id="schoolTable"> <thead> <tr> <th> </th> <th>School Name</th> <th>Country</th> </tr> </thead> <tbody id="schoolTableBody"> <tr class="school-row" style=""> <td><button id="addSchoolBtn" title="Add" style="width:50px">Add</button></td> <td><input type="text" name="sName" id="sName"></td> <td><input type="text" name="Country" id="Country"></td> </tr> <tr data-schoolidx="1" style=""> <td> </td> <td><input type="hidden" name="sName" value="SK">SK</td> <td><input type="hidden" name="Country" value="US">US</td> </tr> <tr data-schoolidx="1" style=""> <td> </td> <td><input type="hidden" name="sName" value="JS">JS</td> <td><input type="hidden" name="Country" value="US">US</td> </tr> <tr data-schoolidx="2" style="display: none;"> <td> </td> <td><input type="hidden" name="sName" value="CAS">CAS</td> <td><input type="hidden" name="Country" value="CA">CA</td> </tr> <tr data-schoolidx="2" style="display: none;"> <td> </td> <td><input type="hidden" name="sName" value="AM">AM</td> <td><input type="hidden" name="Country" value="CA">CA</td> </tr> <tr data-schoolidx="3" style="display: none;"> <td> </td> <td><input type="hidden" name="sName" value="BAS">BAS</td> <td><input type="hidden" name="Country" value="BR">BR</td> </tr> </tbody>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.