如何从 jQuery 集“弹出”或“移动”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6515544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:59:13  来源:igfitidea点击:

How to 'pop' or 'shift' from jQuery set

jquery

提问by cbp

In Javascript, arrays should have methods popand shift.

在 Javascript 中,数组应该有popshift方法。

However, JQuery objects seem to be missing these methods:

但是,JQuery 对象似乎缺少这些方法:

$('div').shift(); // Error, shift is undefined
$('div').pop(); // Error, pop is undefined
$('div').splice(); // Splice is OK actually

I wonder why these functions are missing - after all, the jquery object is just an array.

我想知道为什么缺少这些函数——毕竟,jquery 对象只是一个数组。

What's the easiest way of performing pop and shift functions on jquery objects?

在 jquery 对象上执行 pop 和 shift 函数的最简单方法是什么?

采纳答案by user113716

They're missing because a jQuery object isn't an Array.

它们丢失是因为 jQuery 对象不是数组。

(function( $ ) {
    $.fn.pop = function() {
        var top = this.get(-1);
        this.splice(this.length-1,1);
        return top;
    };

    $.fn.shift = function() {
        var bottom = this.get(0);
        this.splice(0,1);
        return bottom;
    };
})( jQuery );

EDIT:.slice()doesn't modify the original object. Fixed to use .splice()instead.

编辑:.slice()不修改原始对象。固定使用.splice()

回答by ryanve

Your safest bet would be to just use:

您最安全的选择是使用:

[].pop.call($('div'))
[].shift.call($('div'))

If you want to use the exact syntax in your example you can augment jQuery.fn:

如果你想在你的例子中使用确切的语法,你可以增加jQuery.fn

jQuery.fn.pop = [].pop;
jQuery.fn.shift = [].shift;

The latter works well for the mutator methods. It'll work for the accessorand iterationmethods too, but be advised that many of those returns a pure array that you'd have to rewrap. Be aware that jQuery has is own version of some of these (e.g. .map, .slice, .filter, etc.) that you probably don't want to overwrite.

后者适用于mutator 方法。它也适用于访问器迭代方法,但请注意,其中许多方法返回一个您必须重新包装的纯数组。请注意,jQuery 拥有您可能不想覆盖的其中一些(例如.map.slice.filter等)的自己版本。

回答by BM-

This seemed to work for me:

这似乎对我有用:

var divArray = $('div').toArray();
var elem = $( divArray.shift() );

.toArray()return the DOM elements as a JavaScript Array, which can be used as intended. Then all you need to do is convert it back into a jQuery object.

.toArray()将 DOM 元素作为 JavaScript 数组返回,可以按预期使用。然后你需要做的就是将它转换回一个 jQuery 对象。

回答by David Stinemetze

I realize this answer has already been selected, but here's another alternative that isn't too hard to remember, in case you don't want to worry about having to install plugins all the time.

我意识到已经选择了这个答案,但这里有另一种不太难记住的替代方法,以防您不想担心必须一直安装插件。

$('div > :first').detach(); // shift
$('div > :last').detach();  // pop

By the way, I realize there are performance issues with using :last selector as part of your primary selectorso you may want to consider doing something like this for pop:

顺便说一句,我意识到使用 :last 选择器作为主选择器的一部分存在性能问题,因此您可能需要考虑为 pop 执行以下操作:

$('div').children(':last').detach();

回答by jbyrd

var $firstDiv = $( $('div').splice(0, 1) );

回答by dgo.a

Another way using jQuery 1.9.1+:

另一种使用方式jQuery 1.9.1+

$('div').first().remove();  
$('div').last().remove();