【发布时间】:2011-08-24 13:58:46
【问题描述】:
我正在尝试修改 javascript 手风琴菜单以满足我老板的需要。但是,现在我已经进行了大部分调整,但我遇到了虚拟砖墙。函数 expandInit() 不接受我传递给它的参数。我不断收到“预期对象”错误,但我已经使用 typeof() 测试了函数的参数,它返回参数是一个对象。所以我很难过,任何人都可以对此有所帮助。
以下是相关代码:
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 代码)?
-
typeof($(this.contentNode))将始终为object,即使this.contentNode是undefined或null,因为它是一个jQuery 对象。您可能需要查看this.parentNode.activeNode实际上是什么(查看调试器)。如果您需要进一步的帮助,请提供expandInit()的代码。 -
@jfriend00 OP 没有使用 jQuery,在这种情况下,美元函数被声明为 getElementById 的简写
标签: javascript function argument-passing