简体   繁体   English

如何访问不显示的元素属性?

[英]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 . 看看addClassremoveClass 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> 

怎么输入值<textarea>&lt;i&gt;field using puppteer whose style is "display: none" in DOM&lt;/div&gt;&lt;/i&gt;&lt;b&gt;使用 DOM 中样式为 "display: none" 的 puppteer 字段&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我在 DOM 中有一个 textarea 字段。 我想使用 Puppeteer 在文本字段中输入值。</p><pre class="lang-html prettyprint-override"> &lt;textarea id="sample" name ="gsp" style:"display: none"&gt; &lt;/textarea&gt;</pre></div> - how to enter value in the <textarea> field using puppteer whose style is "display: none" in DOM

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从具有属性style =“ display:none;的元素中提取文本; - How to extract text from element with attribute style="display:none; 如何使用“显示:无;” 在标题属性内的 span 元素上? - How to use "display: none;" on a span element which is inside a title attribute? 获取隐藏(display:none)html元素的属性 - Get Attribute of hidden (display:none) html element 如何使用jQuery获取边界不存在的元素对象? - How to get element's object whose border is none using jQuery? 如何访问id可变的元素 - how to access element whose id is variable 如果元素样式显示=“无”,如何捕获 - how to capture if the element style display = “none” 如何通过单击按钮使元素“显示:无” - How to make the element `display: none` by clicking the button Toggle使用display:none淡入一个元素? - ToggleFade an element with display:none? 怎么输入值<textarea>&lt;i&gt;field using puppteer whose style is "display: none" in DOM&lt;/div&gt;&lt;/i&gt;&lt;b&gt;使用 DOM 中样式为 "display: none" 的 puppteer 字段&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我在 DOM 中有一个 textarea 字段。 我想使用 Puppeteer 在文本字段中输入值。</p><pre class="lang-html prettyprint-override"> &lt;textarea id="sample" name ="gsp" style:"display: none"&gt; &lt;/textarea&gt;</pre></div> - how to enter value in the <textarea> field using puppteer whose style is "display: none" in DOM 当元素为(显示:无)时如何显示选中的收音机元素 - how to display elements of checked radio when element are (display: none)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM