2014-02-13 65 views

回答

4

还有就是ancestors method

node.ancestors.each do |a| 
    break if a.attributes['id'].present? 
end 

目前尚不清楚你想要做什么,但你可以通过一个选择器作为参数,所以要找到其中有一个id属性,你可以做到这一切的祖先:

node.ancestors("[@id]") 
1

递归的方法来获得一个包含所有节点到根的数组:

get = proc { |n| [n] + (n.respond_to?(:parent) ? get.(n.parent): []) } 
get.(node)