jQuery - 渲染的 ForEach 函数 HTML

jQuery - ForEach function on rendered HTML

我有以下 JS/jQuery,它获取 ASP DetailsView 控件(呈现 HTML)的单元格值,检查它的条件,并隐藏 div 基于上述条件。它基本上是检查单元格值的格式是否类似于 IP 地址...

$(document).ready(function () {
    //Grab innerHTML in the IPAddress cell from DetailsView1
    //See if it contains the characters: 10.
    //If it does not contain '10.', hide the parent div for that user
    var ip = document.getElementById("DetailsView1").rows[3].cells[0].innerHTML;
    var addrFormat = ip.includes("10.");
    if (addrFormat == false) {
        $("#IDofDetailsView1ParentDiv").hide();
    }
});

这很好用,但我需要检查几个 DetailsView 控件,并可能按 ID .hide() 几个元素。

在 JS/jQuery 中,有没有办法:

ForEach 渲染的 HTML 元素,如 DetailsView,检查格式条件,然后隐藏父级 div,如果条件为 false

感谢您的任何建议。

已编辑以显示标记

这是渲染的 HTML 我正在使用...

<div class="square" id="myId">
<div class="content">
    <div><p id="myId2">Unavailable for Dispatch</p>
        <div class="table">
            <div class="table-cell">
                <div id="UpdatePanel1">
            <div>
        <table class="Grid" cellspacing="0" rules="all" border="1" id="DetailsView1" style="border-collapse:collapse;">
        <tr align="center">
            <td colspan="2" style="border-style:None;">Text1</td>
        </tr><tr align="center">
            <td class="building" colspan="2" style="border-style:None;">Text2</td>
        </tr><tr align="center">
            <td colspan="2" style="border-style:None;">Text3</td>
        </tr><tr align="center">
            <td class="hide" colspan="2" style="border-style:None;">Text4</td>
        </tr><tr align="center">
            <td class="hide" colspan="2">Text5</td>
        </tr>
        </table>
        </div>
                </div>
            </div>
        </div>
    </div>
</div>

我的页面上有几个。现在我正在使用:

var ip = document.getElementById("DetailsView1").rows[3].cells[0].innerHTML;

但我可能需要在 "Grid" 上使用 class 选择器来同时处理它们,对吗?

然后我需要通过 id 隐藏最高的 div,而不是最近的父级。但是只隐藏那些 td 'Text 4' 值为 false 的 divs。如果我改用网格 class,我不确定如何获取当前从 "DetailsView1" 获取的内部 HTML。

再次感谢...

用于每个,然后使用 'this' 定位单个元素。

编辑:必须对此进行编辑并删除 includes() 因为它在很多浏览器中不起作用。现在我将所有 .Grid 元素的 HTML 转换为 String() ,然后我在该字符串上使用 indexOf() 来确定它是否包含 10.

新工作fiddle: https://jsfiddle.net/Lh5kenge/

更新代码:

   $(function(){
          $('.Grid').each(function(){
          var string = $(this).html().toString()
          console.log(string)
            if (string.indexOf("10.") > -1) {
              $(this).closest('.square').hide()
            }
            })
            })