jQuery在使用find函数时,它的子元素返回未定义的属性

时间:2017-06-15 11:13:36

标签: javascript jquery



$("table.overview-table td.orange.white").each(function(){
    if($(this).attr("rowspan") > 0) {
        var childrens = $(this).parent().next("tr").find("*");
        for(x = 0; x < childrens.length; x++) {
            console.log(childrens[x].attr("workerid"));
        }
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-responsive table-bordered overview-table" border=1>
   <thead>
      <tr>
         <th class="dark" style="width: 50px;">Tijd</th>
         <th class="dark">Kapper 1</th>
         <th class="dark">Kapper 2</th>
      </tr>
   </thead>
   <tbody class="overview_table_td">
      <tr>
         <th scope="row">03:15</th>
         <td class="grey" onclick="make_app("2017-06-19","03:15","Kapper 1","148","1","1")"></td><td class="orange" onclick="make_app("2017-06-19","03:15","Kapper 2","196","1","0")" rowspan="0" data-time="03:15:00" workerid="196"></td>
      </tr>
      <tr>
         <th scope="row" class="dark">03:30</th>
         <td class="grey" onclick="make_app("2017-06-19","03:30","Kapper 1","148","1","1")"></td><td class="orange white" onclick="show_app("4614")" rowspan="2" data-time="03:30:00" workerid="196">test</td>
      </tr>
      <tr>
         <th scope="row">03:45</th>
         <td class="grey" onclick="make_app("2017-06-19","03:45","Kapper 1","148","1","1")"></td><td class="orange" onclick="make_app("2017-06-19","03:45","Kapper 2","196","1","0")" rowspan="0" data-time="03:45:00" workerid="196"></td>
      </tr>
   </tbody>
</table>
&#13;
&#13;
&#13;

当我获取子元素的属性时,显示错误正在显示

未捕获的TypeError:childrens [x] .attr不是函数

寻求帮助

1 个答案:

答案 0 :(得分:1)

您应该使用$(childrens[x]).attr("workerid")代替 childrens[x].attr("worked")

$(element)选择HTML元素,以便您可以通过jquery获取/设置该元素的属性/属性。

function rowspan_treatment(){
$("table.overview-table td.orange.white").each(function(){
    if($(this).attr("rowspan") > 0 && $(this).attr("rowspan") !== "undefined") {
        var childrens = $(this).parent().next("tr").find("*");
        for(x = 0; x < childrens.length; x++) {
            console.log($(childrens[x]).attr("workerid"));
        }
    }
});
}

&#13;
&#13;
$("table.overview-table td.orange.white").each(function(){
    if($(this).attr("rowspan") > 0) {
        var childrens = $(this).parent().next("tr").find("*");
        for(x = 0; x < childrens.length; x++) {
        
            if($(childrens[x]).attr("workerid") != undefined){
            
            console.log($(childrens[x]).attr("workerid"));
            }
        }
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-responsive table-bordered overview-table" border=1>
   <thead>
      <tr>
         <th class="dark" style="width: 50px;">Tijd</th>
         <th class="dark">Kapper 1</th>
         <th class="dark">Kapper 2</th>
      </tr>
   </thead>
   <tbody class="overview_table_td">
      <tr>
         <th scope="row">03:15</th>
         <td class="grey" onclick="make_app("2017-06-19","03:15","Kapper 1","148","1","1")"></td><td class="orange" onclick="make_app("2017-06-19","03:15","Kapper 2","196","1","0")" rowspan="0" data-time="03:15:00" workerid="196"></td>
      </tr>
      <tr>
         <th scope="row" class="dark">03:30</th>
         <td class="grey" onclick="make_app("2017-06-19","03:30","Kapper 1","148","1","1")"></td><td class="orange white" onclick="show_app("4614")" rowspan="2" data-time="03:30:00" workerid="196">test</td>
      </tr>
      <tr>
         <th scope="row">03:45</th>
         <td class="grey" onclick="make_app("2017-06-19","03:45","Kapper 1","148","1","1")"></td><td class="orange" onclick="make_app("2017-06-19","03:45","Kapper 2","196","1","0")" rowspan="0" data-time="03:45:00" workerid="196"></td>
      </tr>
   </tbody>
</table>
&#13;
&#13;
&#13;