Это будет делать то, что вы хотите. Инструкция console.log предназначена для Firebug. Просто замените его вашим кодом, который использует массив.
$(".leaf").click(function(){
var $this = $(this),
array = $.map(
$this.add($this.parents(':not(:has(div.top))')),
function(n){ return n.className }
).reverse();
console.log( array );
});
Выводит это при нажатии:
["top", "branch", "branch", "leaf"]
Обновление
Чтобы исключить top
из результата, просто немного измените селектор:
$(".leaf").click(function(){
var $this = $(this),
array = $.map(
$this.add($this.parents(':not(:has(div.top)):not(div.top)')),
function(n){ return n.className }
).reverse();
console.log( array );
});
Выводит это при нажатии:
["branch", "branch", "leaf"]