如何从给定两个类之间的表中获取所有tr和td?

时间:2017-10-05 17:33:24

标签: javascript jquery html-table

我的table具有以下格式:

<table id="permissions" class="sticky-enabled tableheader-processed sticky-table permissions-processed">
    <thead>
        <tr>
            <th>Permission</th>
            <th class="checkbox">Staff</th>
        </tr>
    </thead>
    <tbody>
        <tr class="even">
            <td class="module" id="module-heartbeat_ui" colspan="2">Heartbeat UI</td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-admin-heartbeat-configure" class="form-item form-type-item">
                    Configure heartbeat
                    <div class="description">Manage the heartbeat streams and configuration settings.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-admin-heartbeat-configure">
                    <label class="element-invisible" for="edit-4-admin-heartbeat-configure">Staff: Configure heartbeat </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-admin-heartbeat-configure" name="4[admin heartbeat configure]" value="admin heartbeat configure"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>

        <tr class="odd">
            <td class="module" id="module-heartbeat" colspan="2">Heartbeat activity</td>
        </tr>
        <tr class="even">
            <td class="permission">
                <div id="edit-admin-heartbeat-templates" class="form-item form-type-item">
                    Administer heartbeat templates
                    <div class="description">Manage the heartbeat templates.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-admin-heartbeat-templates">
                    <label class="element-invisible" for="edit-4-admin-heartbeat-templates">Staff: Administer heartbeat templates </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-admin-heartbeat-templates" name="4[admin heartbeat templates]" value="admin heartbeat templates"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-admin-heartbeat-delete-all" class="form-item form-type-item">
                    Delete all activity
                    <div class="description">Master permission to delete all activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-admin-heartbeat-delete-all">
                    <label class="element-invisible" for="edit-4-admin-heartbeat-delete-all">Staff: Delete all activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-admin-heartbeat-delete-all" name="4[admin heartbeat delete all]" value="admin heartbeat delete all"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="even">
            <td class="permission">
                <div id="edit-admin-heartbeat-delete-own" class="form-item form-type-item">
                    Delete own activity
                    <div class="description">Permission for the actor to delete own activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-admin-heartbeat-delete-own">
                    <label class="element-invisible" for="edit-4-admin-heartbeat-delete-own">Staff: Delete own activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-admin-heartbeat-delete-own" name="4[admin heartbeat delete own]" value="admin heartbeat delete own"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-view-heartbeat-messages" class="form-item form-type-item">
                    View activity
                    <div class="description">Global permission to view heartbeat activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-view-heartbeat-messages">
                    <label class="element-invisible" for="edit-4-view-heartbeat-messages">Staff: View activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-view-heartbeat-messages" name="4[view heartbeat messages]" value="view heartbeat messages"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="even">
            <td class="permission">
                <div id="edit-access-heartbeat-activity-profiles" class="form-item form-type-item">
                    Access heartbeat activity profiles
                    <div class="description">Permission to see user profiles or links to the user profile.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-access-heartbeat-activity-profiles">
                    <label class="element-invisible" for="edit-4-access-heartbeat-activity-profiles">Staff: Access heartbeat activity profiles </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-access-heartbeat-activity-profiles" name="4[access heartbeat activity profiles]" value="access heartbeat activity profiles"><input type="checkbox" class="dummy-checkbox"
                        disabled="disabled" checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-view-single-activity-stream" class="form-item form-type-item">
                    View activity in Single activity
                    <div class="description">Stream access: Single activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-view-Single-activity-stream">
                    <label class="element-invisible" for="edit-4-view-single-activity-stream">Staff: View activity in Single activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-view-single-activity-stream" name="4[view Single activity stream]" value="view Single activity stream"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="even">
            <td class="permission">
                <div id="edit-view-views-activity-stream" class="form-item form-type-item">
                    View activity in Views activity
                    <div class="description">Stream access: Views activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-view-Views-activity-stream">
                    <label class="element-invisible" for="edit-4-view-views-activity-stream">Staff: View activity in Views activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-view-views-activity-stream" name="4[view Views activity stream]" value="view Views activity stream"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>

        <tr class="odd">
            <td class="module" id="module-hide_modules" colspan="2">Hide Modules</td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-edit-webform-components" class="form-item form-type-item">
                    Content authors: access and edit webform components and settings
                    <div class="description">Grants additional access to the webform components and settings to users who can edit the content. Generally an authenticated user permission.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-edit-webform-components">
                    <label class="element-invisible" for="edit-4-edit-webform-components">Staff: Content authors: access and edit webform components and settings </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-edit-webform-components" name="4[edit webform components]" value="edit webform components" checked="checked"><input type="checkbox" class="dummy-checkbox"
                        disabled="disabled" checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
    </tbody>
</table>

我需要使用jQuery在tr的{​​{1}}和td之间找到表格的所有class="module" id="module-heartbeat_ui"。是否有任何简写的jQuery代码来抓取class="module" id="module-heartbeat"tr之间的那些事件。

2 个答案:

答案 0 :(得分:1)

听起来你想使用nextUtil

&#13;
&#13;
$('tr:has("td#module-heartbeat_ui")').nextUntil('tr:has("td#module-heartbeat")').addBack().find("td").css("background-color","yellow")
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="permissions" class="sticky-enabled tableheader-processed sticky-table permissions-processed">
    <thead>
        <tr>
            <th>Permission</th>
            <th class="checkbox">Staff</th>
        </tr>
    </thead>
    <tbody>
        <tr class="even">
            <td class="module" id="module-heartbeat_ui" colspan="2">Heartbeat UI</td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-admin-heartbeat-configure" class="form-item form-type-item">
                    Configure heartbeat
                    <div class="description">Manage the heartbeat streams and configuration settings.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-admin-heartbeat-configure">
                    <label class="element-invisible" for="edit-4-admin-heartbeat-configure">Staff: Configure heartbeat </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-admin-heartbeat-configure" name="4[admin heartbeat configure]" value="admin heartbeat configure"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>

        <tr class="odd">
            <td class="module" id="module-heartbeat" colspan="2">Heartbeat activity</td>
        </tr>
        <tr class="even">
            <td class="permission">
                <div id="edit-admin-heartbeat-templates" class="form-item form-type-item">
                    Administer heartbeat templates
                    <div class="description">Manage the heartbeat templates.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-admin-heartbeat-templates">
                    <label class="element-invisible" for="edit-4-admin-heartbeat-templates">Staff: Administer heartbeat templates </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-admin-heartbeat-templates" name="4[admin heartbeat templates]" value="admin heartbeat templates"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-admin-heartbeat-delete-all" class="form-item form-type-item">
                    Delete all activity
                    <div class="description">Master permission to delete all activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-admin-heartbeat-delete-all">
                    <label class="element-invisible" for="edit-4-admin-heartbeat-delete-all">Staff: Delete all activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-admin-heartbeat-delete-all" name="4[admin heartbeat delete all]" value="admin heartbeat delete all"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="even">
            <td class="permission">
                <div id="edit-admin-heartbeat-delete-own" class="form-item form-type-item">
                    Delete own activity
                    <div class="description">Permission for the actor to delete own activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-admin-heartbeat-delete-own">
                    <label class="element-invisible" for="edit-4-admin-heartbeat-delete-own">Staff: Delete own activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-admin-heartbeat-delete-own" name="4[admin heartbeat delete own]" value="admin heartbeat delete own"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-view-heartbeat-messages" class="form-item form-type-item">
                    View activity
                    <div class="description">Global permission to view heartbeat activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-view-heartbeat-messages">
                    <label class="element-invisible" for="edit-4-view-heartbeat-messages">Staff: View activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-view-heartbeat-messages" name="4[view heartbeat messages]" value="view heartbeat messages"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="even">
            <td class="permission">
                <div id="edit-access-heartbeat-activity-profiles" class="form-item form-type-item">
                    Access heartbeat activity profiles
                    <div class="description">Permission to see user profiles or links to the user profile.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-access-heartbeat-activity-profiles">
                    <label class="element-invisible" for="edit-4-access-heartbeat-activity-profiles">Staff: Access heartbeat activity profiles </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-access-heartbeat-activity-profiles" name="4[access heartbeat activity profiles]" value="access heartbeat activity profiles"><input type="checkbox" class="dummy-checkbox"
                        disabled="disabled" checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-view-single-activity-stream" class="form-item form-type-item">
                    View activity in Single activity
                    <div class="description">Stream access: Single activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-view-Single-activity-stream">
                    <label class="element-invisible" for="edit-4-view-single-activity-stream">Staff: View activity in Single activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-view-single-activity-stream" name="4[view Single activity stream]" value="view Single activity stream"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
        <tr class="even">
            <td class="permission">
                <div id="edit-view-views-activity-stream" class="form-item form-type-item">
                    View activity in Views activity
                    <div class="description">Stream access: Views activity.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-view-Views-activity-stream">
                    <label class="element-invisible" for="edit-4-view-views-activity-stream">Staff: View activity in Views activity </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-view-views-activity-stream" name="4[view Views activity stream]" value="view Views activity stream"><input type="checkbox" class="dummy-checkbox" disabled="disabled"
                        checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>

        <tr class="odd">
            <td class="module" id="module-hide_modules" colspan="2">Hide Modules</td>
        </tr>
        <tr class="odd">
            <td class="permission">
                <div id="edit-edit-webform-components" class="form-item form-type-item">
                    Content authors: access and edit webform components and settings
                    <div class="description">Grants additional access to the webform components and settings to users who can edit the content. Generally an authenticated user permission.</div>
                </div>
            </td>
            <td class="checkbox">
                <div class="form-item form-type-checkbox form-item-4-edit-webform-components">
                    <label class="element-invisible" for="edit-4-edit-webform-components">Staff: Content authors: access and edit webform components and settings </label>
                    <input class="rid-4 form-checkbox real-checkbox" type="checkbox" id="edit-4-edit-webform-components" name="4[edit webform components]" value="edit webform components" checked="checked"><input type="checkbox" class="dummy-checkbox"
                        disabled="disabled" checked="checked" title="This permission is inherited from the authenticated user role." style="display: none;">
                </div>
            </td>
        </tr>
    </tbody>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:-2)

在JavaScript中,您可以获取表格元素HTML代码并进行拆分   <tr> 然后循环每个tr以获得td的