JQuery控制台错误

时间:2013-12-14 19:11:18

标签: javascript jquery

我正在尝试使用菜单控制内容滑块。 Div元素将放在包装器中,我正在使用脚本更改其位置。请检查以下代码

jQuery(document).ready(function() {
    function goto(id){
        jQuery(".contentbox-wrapper").animate({"left": -(jQuery(id).position().left)}, 600);
    }
});   

和html函数调用,

<li><a href="#" onClick="goto('#homePage', this); return false">Home</a></li>

<li><a href="#" onClick="goto('#aboutPage', this); return false">About</a></li>

,内容div就像

<div class="contentbox-wrapper">
    <div id="homePage" class="contentBox">
        content here
    </div>
    <div id="aboutPage" class="contentBox">
        content here
    </div>
</div>

问题是,当我点击菜单项时,我收到此错误

“未捕获的ReferenceError:goto未定义”

请帮助..

2 个答案:

答案 0 :(得分:1)

您应该将goto函数放在document ready事件处理函数之外。 你的功能也是一个参数,但你每次调用它时都会使用两个(不是这样,只是没有多大意义)。

答案 1 :(得分:0)

当您在链接的onclick属性中引用它时,您传递了两个参数,但在其定义中,它只接受一个参数,即id。