这是一个有两个部分的问题。首先是标题的问题。以下是我所了解到的内容:
// Report all of the parents
$(this).parents().each(function(i){
// Collect the parts in a var
var $crumb = '';
// Get the tag name of the parent
$crumb += "<span class='tagName'>"+this.tagName+"</span>";
// And finally, report it
$breadcrumbs.prepend($crumb);
});
不幸的是,这并不包括实际的元素本身,只有其父元素。是否有一种方法可以说出类似于“这个和它的父元素”?
现在,第二个问题。如果我无法添加到堆栈中,那么我如何将该函数的主体分离到另一个函数中,同时保留其“this”能力?是否应该像这样:
// Function to report the findings
function crumble(e){
// Collect the parts in a var
var $crumb = '';
// Get the tag name of the parent
$crumb += "<span class='tagName'>"+this.tagName+"</span>";
// And finally, report it
$breadcrumbs.prepend($crumb);
};
$(this).parents().each(crumble());
非常感谢您抽出时间!