背景:我试图编辑一个禅宗购物车水平弹出菜单,使弹出菜单内嵌在菜单内。我遇到的问题是我正在努力让自己的头脑围绕它附带的javascript/jquery。困惑 - 获取错误“this.myfuntion()不是一个函数”
没有张贴了整个事情的代码的结构是这样的:
(declare some vars)
//some functions like this:
function funcname(obj) {
//do something
}
//then one big master function like this:
function bigfunc(arg1, arg2, arg3, arg4, arg5) {
//declare some vars based on this
this.varname1=varname1;
this.varname2=varname2;
//declare some functions inside the big function
this.innerfunc1= function() {
//do stuff
}
this.innerfunc2= function() {
//do stuff
}
}//end of big function
//then goes on to declare init function
function initfunc(){
//this creates new bigfunc(arg1 arg2 arg3...) for each main menu item
}
//finally calls init function with
window.onload = initfunc();
现在到我的困惑 -
1)首先澄清,我在思考基础上正确的这在bigfunc()中是浮动的,而且它是用new bigfunc()调用的,这是创建一个对象的事实?
2)我现在的问题是与内部bigfunc的功能之一(),它看起来像这样:
this.slideChildMenu = function() {
var divref = this.children[0].div;
var ulref = this.children[0].ul;
var maxwidth = this.children[0].width;
var nextWidth;
if (this.isMouseOnMe || this.isMouseOnChild()) {
nextWidth = divref.offsetWidth + slideSpeed_out;
if (nextWidth >= maxwidth) {
this.finishOpeningChild(divref, ulref, maxwidth);
} else {
ulref.style.left = nextWidth - maxwidth + "px";
divref.style.width = nextWidth + "px";
setTimeout("slideChildMenu('" + this.getId() + "')", slideTimeout_out);
}
}
现在我的计划是要改变这种使用jquery显示打开元素,所以我想这:
this.slideChildMenu = function() {
var divref = this.children[0].div;
var ulref = this.children[0].ul;
if (this.isMouseOnMe || this.isMouseOnChild()) {
$(divref).show(function(){
this.finishOpeningChild(divref, ulref);
});
}
}
但我得到这个 - >类型错误:this.finishOpeningChild不是一个函数
现在,有很多其他的东西在这个事情JS所以我不会博士在这里问某人为我做我的工作,但我希望如果有人能向我解释为什么这个功能不是一个功能,我可能能够解决其他问题。
注意:我认为这与“this”的范围有关,但是这两个版本的代码中的值似乎完全相同。
我知道这是一个很长的,但你的帮助,不胜感激。
谢谢,这正是我所需要的 - 没有更多的错误...仍然无法正常工作,但我想我现在有机会修复它。再次感谢。 – WebweaverD 2012-08-08 21:03:19
不错的建议。我已经完成了它,因为它可以让您在更改之前捕获当前范围。 +1! – PJH 2012-08-08 21:34:49