Ecco un'estensione del prototipo jQuery ($ .fn) oggetto di fornire un nuovo metodo che può essere incatenato alla funzione jQuery().
Avevo bisogno di funzionalità in cui avevo bisogno di aggiungere un elemento tra la lista che ho diviso. Questo è stato aggiunto come parametro opzionale.
Un esempio è disponibile presso http://jsfiddle.net/roeburg/5F2hW/
L'utilizzo della funzione è in questo modo:
$("ul").customSplitList(5);
La funzione è definita come segue:
// Function definition
(function ($) {
// Function is defined here ...
$.fn.customSplitList = function (indexToSplit, elementToAddInBetween) {
// Holds a reference to the element(list)
var that = this;
var subList, newList, listLength;
// Only continue if the element is a derivitive of a list
if ($(that) && ($(that).is("ul") || $(that).is("ol"))) {
// Additionally check if the length & the split index is valid
listLength = $(that).children().length;
if ($.isNumeric(indexToSplit) && indexToSplit > 0 && indexToSplit < listLength) {
// Based on list type, create a new empty list
newList = $($(that).clone(true)).empty();
while ((subList = this.find('li:gt(' + (indexToSplit - 1) + ')').remove()).length) {
newList.append(subList);
}
if (elementToAddInBetween && $(elementToAddInBetween)) {
that.after(newList);
newList.before(elementToAddInBetween);
} else {
that.after(newList);
}
}
}
};
})(jQuery);
Spero che questo aiuti.
fonte
2014-07-09 08:24:46
V'è una libreria che fa questo - https://github.com/yairEO/listBreaker – vsync