我有以下代码:
var parentEls = node.parents()
.map(function () {
var curParentID = this.getAttribute("id");
var curParentClass = this.getAttribute("class");
if(curParentID) {
return this.tagName + "#" + curParentID;
/*stop the map function from proceeding*/
} else {
return this.tagName;
}
})
.get().reverse().join(", ");
上面的代码有助于查找唯一 ID 的搜索,一旦发现 ID,它就会创建 xpath。一旦到达检索到 id 并且 map 功能应该停止的点,我如何停止 map 功能?我试过 return false 和 break 但它不起作用。
还有什么建议吗?
最佳答案
我认为使用 .map() 是不可能的,你可能必须在这里使用 .each()
var temp = [];
node.parents().each(function () {
var curParentID = this.getAttribute("id");
var curParentClass = this.getAttribute("class");
if(curParentID){
temp.push(this.tagName + "#" + curParentID);
return false;
} else {
temp.push(this.tagName);
}
});
var parentEls = temp.reverse().join(", ");
关于javascript - 满足条件时停止 Jquery.map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18180875/