我想修改一个JavaScript的手风琴菜单,以适应我的老板的需求。但是,现在我已经做了大部分的调整,我已经打了一个虚拟的砖墙。函数expandInit()不接受我传递给它的参数。我不断收到“Object expected”错误,但我已经使用typeof()测试了函数的参数,并且它返回参数是一个Object。所以我很难过,任何人都可以放弃的任何光线都会非常有帮助。javascript函数不接受通过对象变量
下面是相关代码:
function $(id){
return document.getElementById(id);
}
function expandTimer(div){
if(getHeight(div) < div.maxh){
var velocity = Math.round((div.maxh - getHeight(div))/div.speed);
velocity = (velocity < 1) ? 1 : v;
velocity = (getHeight(div) + velocity);
setHeight(div, velocity+'px');
div.style.opacity = (velocity/div.maxh);
div.style.filter = 'alpha(opacity = '+(velocity * 100/div.maxh)+');';
} else {
setHeight(div, div.maxh);
clearInterval(div.refRate);
}
}
//arg types are String, Int, and, String.
function accordian(mother, speed, alternate){
var motherObj = $(mother);
motherObj.activeNode='';
var allDivs = motherObj.getElementsByTagName('div');
for (var i = 0; i < allDivs.length; i++){
var divID = allDivs[i].id;
if (divID.substr(divID.indexOf('-') + 1, divID.length) == 'header'){
var contentID = divID.substr(0, divID.indexOf('-'))+'-content';
var divObj = $(divID);
divObj.contentNode = contentID;
divObj.hightlight = alternate;
var contentObj = $(contentID);
contentObj.style.display = 'none';
contentObj.style.overflow = 'hidden';
contentObj.maxh = getHeight(contentObj);
contentObj.speed = speed;
contentObj.refRate;
divObj.onmouseover = function(){
if (this.parentNode.activeNode != ''){
collapseInit($(this.parentNode.activeNode));
}
alert (typeof($(this.contentNode))); //returns 'object'
//code executes fine up until here.
expandInit($(this.contentNode));
this.parentNode.activeNode = this.contentNode;
}
}
}
}
虽然我没有包括collapseInit()的代码,它有同样的问题。只要说上面提到的所有其他功能按预期工作即可。
编辑:对不起,没有包含正确的功能。下面是expandInit()函数:
function expandInit(div){
alert("in expandInit"); //<---does not execute.
setDisplay(div,'block');
div.style.height='0px';
clearInterval(div.refRate);
div.refRate = setInterval('expandTimer('+div+')', 10);
}
什么是'expandInit'?你能链接或粘贴定义吗? –
您是否清除浏览器缓存(它可能保留了旧的Js代码)? – VirtualTroll
'typeof($(this.contentNode))'将始终为'object',即使'this.contentNode'为undefined或null,因为它是一个jQuery对象。你可能需要看看'this.parentNode.activeNode'实际上是什么(查看调试器)。如果您需要进一步的帮助,请包含'expandInit()'的代码。 – jfriend00