Jquery - 使用Class中的Class获取DIV文本

时间:2014-10-31 08:59:03

标签: jquery

以下是网格中Headers的代码。我想迭代divui-grid-cell-contents,它位于类ui-grid-header-cell内,以获取所有列标题&将其保存在数组对象中。

<div class="ui-grid-header-cell clearfix ng-scope ng-isolate-scope ui-grid-coluiGrid-002">
    <div class="ng-scope sortable">
        <div class="ui-grid-vertical-bar">&nbsp;</div>
        <div class="ui-grid-cell-contents ng-binding" col-index="renderIndex">
            Code <span ui-grid-visible="col.sort.direction" ng-class="{ 'ui-grid-icon-up-dir': col.sort.direction == asc, 'ui-grid-icon-down-dir': col.sort.direction == desc, 'ui-grid-icon-blank': !col.sort.direction }" class="ui-grid-icon-up-dir">&nbsp;</span>
        </div>
        <div class="ui-grid-column-menu-button ng-scope" ng-if="grid.options.enableColumnMenus &amp;&amp; !col.isRowHeader  &amp;&amp; col.colDef.enableColumnMenu !== false" ng-click="toggleMenu($event)"><i class="ui-grid-icon-angle-down">&nbsp;</i></div>
    </div>
</div>

<div class="ui-grid-header-cell clearfix ng-scope ng-isolate-scope ui-grid-coluiGrid-002">
    <div class="ng-scope sortable">
        <div class="ui-grid-vertical-bar">&nbsp;</div>
        <div class="ui-grid-cell-contents ng-binding" col-index="renderIndex">
            EMPLOYEE NAME<span ui-grid-visible="col.sort.direction" ng-class="{ 'ui-grid-icon-up-dir': col.sort.direction == asc, 'ui-grid-icon-down-dir': col.sort.direction == desc, 'ui-grid-icon-blank': !col.sort.direction }" class="ui-grid-icon-up-dir">&nbsp;</span>
        </div>
        <div class="ui-grid-column-menu-button ng-scope" ng-if="grid.options.enableColumnMenus &amp;&amp; !col.isRowHeader  &amp;&amp; col.colDef.enableColumnMenu !== false" ng-click="toggleMenu($event)"><i class="ui-grid-icon-angle-down">&nbsp;</i></div>
    </div>
</div>

.......

我试过了: -

jq(".ui-grid-header-cell > .ui-grid-cell-contents").each(function (index, element) {
                    console.log(element);        
                });

不会给出预期的结果。

1 个答案:

答案 0 :(得分:2)

.ui-grid-cell-contents元素不是.ui-grid-header-cell的直接后代,尽管它是一个孩子。只需从选择器中省略>

jq(".ui-grid-header-cell .ui-grid-cell-contents").each(function (index, element) {
    console.log(element);        
});

Here's your jsFiddle