简体   繁体   English

jQuery最后一个子选择器

[英]jQuery last child selector

This is not working and I don't know why. 这不起作用,我也不知道为什么。 I have tried it in the console it works, but when I change my js codes it does not work. 我已经在控制台中尝试了它,但是当我更改我的js代码时,它不起作用。

$('#recipe-table td:last-child').width();

How can I get it the correct way? 如何获得正确的方法?

this is the html 这是HTML

           <div id="div-lists">  
                    <div id="div-recipe-table">
                    <table class="" id="recipe-table" cellspacing="0" cellpadding="0">
                        <tbody>                                
                                    <tr class="recipe-list" data-ng-repeat="recipe in recipe_data" ng-click="recipe_click(recipe.CodeListe,recipe.Name)">
                                        <td id="td-img{{recipe.CodeListe}}">
                                            <center>
                                                <span id="img{{recipe.ID}}X">
                                                    <a id="{{recipe.ID}}" href="javascript:void(0);">
                                                        <img  ng-if="recipe.Pictures !==''" fallback-src="images/default.png" ng-src="{{recipe.Pictures}}" class="images" id="img{{recipe.ID}}"/><img  ng-if="recipe.Pictures===''" class="images" src="images/default.png" id="img1"/>
                                                    </a>
                                                </span>
                                            </center>
                                        </td>
                                 <!-- I want to get The width of this <td> -->
                                        <td class="recipes"> 
                                            <div id="div-recipe-name">
                                                <strong>{{recipe.Name}}</strong></div>
                                        </td>
                                 <!-- Until here
                                    I already tried using $('.recipes').width(); but it returns 0 -->
                                    </tr>
                        </tbody>
                    </table>
                    </div>
                    <div id="div-list-loading"></div>
                    <div id="div-list-noresults">{{ui_translation.UIT[171610]}}</div>
                </div>
            </div>

Try below jQuery 试试下面的jQuery

You can use .last() method constructs a new jQuery object from the last element in that set. 您可以使用.last()方法从该集合的最后一个元素构造一个新的jQuery对象。

$('#recipe-table td').last().width();

JSFIDDLE DEMO JSFIDDLE演示

Try the following: 请尝试以下操作:

$('#recipe-table td:last').width();

From the jQuery Docs jQuery文档

While :last matches only a single element, :last-child can match more than one: one for each parent. :last仅匹配一个元素,而:last-child可以匹配多个:每个父元素匹配一个。

Since you have already tried $('.recipes').width(); 由于您已经尝试过$('.recipes').width(); does the following provide you any results: 以下内容可为您提供任何结果:

$('.recipes').each(function (index, elem) {
    alert($(this).width());
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM