我有一个函数,我使用.each()在jQuery对象上运行。除非,如果某个测试通过,我实际上想要在当前迭代中的节点的父节点上运行该函数。但是,当我尝试切换上下文时,我得到一个赋值错误或无限循环。
以下代码不起作用:
$(this).each(function() {
if($(this).attr("title")) {
content = $(this).attr("title");
$(this) = $(this).parent();
} else {
content = $(this).html();
}
//other stuff
}
答案 0 :(得分:1)
很抱歉,但是你必须从中做出变量。
$(this).each(function() {
var mycontext;
if($(this).attr("title")) {
content = $(this).attr("title");
mycontext = $(this).parent();
} else {
mycontext = $(this);
content = $(this).html();
}
//other stuff using mycontext instead of $(this)
}