jQuery 最后一个元素


    $(':input').blur(function () {
            $(this).css('border', 'solid 1px #ccc');
            // Check if last input and do line below
            //if(condition if $(this) is last element)
            //   $('#someOtherId').focus();
        });

在上面的代码中,如何知道id $(this)是所有选定输入的最后一个输入?

尝试如下,

var $input = $(':input');
$input.blur(function () {
    $(this).css('border', 'solid 1px #ccc');
    // Check if last input and do line below
    if ($input.index(this) == $input.length - 1) {
         //$('#someOtherId').focus();
    }
});

演示:http://jsfiddle.net/skram/eYZU5/3/

试试这个:

$(':input').blur(function() {
    if ($('input:last').is(this)) {
        // do something with last
        $(this).css('color', 'red');
    }
    $(this).css('border', 'solid 1px #ccc');
});

工作示例

我不确定"所有选定的输入"是什么意思,但您可以使用.is():last选择器进行快速检查。

$(':input').blur(function () {
    var _this = $(this);
    if (_this.is(':last')) {
        // do something
    }
});

您可能还想查看 :last-child ,如果这更符合您的要求。

相关内容

最新更新