为什么JsFiddle给出了未定义的错误? [英] Why is JsFiddle giving not defined error?

查看:93
本文介绍了为什么JsFiddle给出了未定义的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个小提琴 http://jsfiddle.net/fXfSz/:

如果您在控制台中查看,则shiftLeft是未定义的,但其定义如下:

If you look in the console it shiftLeft is undefined but it is defined like so:

function shiftLeft()
{
    for (var char in $('#chars').children())
    {
        log(char);
        char.css({left:char.position().left -100});
    }
}

推荐答案

您的shiftLeft函数不是在全局范围内定义的,而是在onload事件处理程序之一中定义的.

Your shiftLeft function isn't defined in the global scope but in the one of the onload event handler.

将其从onload功能代码中删除,并将提琴包装的设置更改为"no wrap-in head".或者,最好使用click函数将其绑定.

Remove it from the onload function code and change the fiddle wrapping setting to "no wrap - in head". Or, better, bind it using the click function.

但是您的函数中还有其他错误.也许你想要这个:

But you have other bugs in your function. Maybe you want this :

<button id="idofthebutton">Left</button>
<script>
$('#idofthebutton').click(function(){
    $('#chars').children().each(function(){
         $(this).css({left:$(this).position().left -100});
    });
});
</script>

演示

这篇关于为什么JsFiddle给出了未定义的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆