javascript - 为什么找不到这个 JavaScript 函数

标签 javascript jquery

我有一些应该在 document.ready 上运行的 JavaScript 代码,但我在 firebug 中一直收到错误,提示找不到 onLoad() 方法.

这是代码(清楚地显示了那里的方法):

<script type="text/javascript">

    var tl;
    function onLoad1() {

        debugger;

        var eventSource = new Timeline.DefaultEventSource(0);

        // Example of changing the theme from the defaults
        // The default theme is defined in 
        // http://simile-widgets.googlecode.com/svn/timeline/tags/latest/src/webapp/api/scripts/themes.js
        var theme = Timeline.ClassicTheme.create();
        theme.event.bubble.width = 350;
        theme.event.bubble.height = 300;

        var d = Timeline.DateTime.parseGregorianDateTime("1900")
        var bandInfos = [
            Timeline.createBandInfo({
                width: "80%",
                intervalUnit: Timeline.DateTime.DECADE,
                intervalPixels: 200,
                eventSource: eventSource,
                date: d,
                theme: theme,
                layout: 'original'  // original, overview, detailed
            }),
            Timeline.createBandInfo({
                width: "20%",
                intervalUnit: Timeline.DateTime.CENTURY,
                intervalPixels: 200,
                eventSource: eventSource,
                date: d,
                theme: theme,
                layout: 'overview'  // original, overview, detailed
            })
        ];
        bandInfos[1].syncWith = 0;
        bandInfos[1].highlight = true;
        
        debugger;

        tl = Timeline.create(document.getElementById("tl"), bandInfos, Timeline.HORIZONTAL);
        // Adding the date to the url stops browser caching of data during testing or if
        // the data source is a dynamic query...
        var jsonFile = "<%= Url.Content("~/scripts/Views/Business/")%>test.js?"+ (new Date().getTime());

        tl.loadJSON(jsonFile), function(json, url) {
            eventSource.loadJSON(json, url);
        });
    }
    var resizeTimerID = null;
    function onResize() {
        if (resizeTimerID == null) {
            resizeTimerID = window.setTimeout(function() {
                resizeTimerID = null;
                tl.layout();
            }, 500);
        }
    }
</script>

<script type="text/javascript">

    $(document).ready(function() {
        debugger;
        onLoad1();
    });
    </script>

知道为什么它不能“找到”那个方法。

编辑

  • 我把所有的 JavaScript 放在上面(删除了图片)。
  • 我将其重命名为 onLoad1() 但我仍然遇到同样的问题。
  • 我将第一段代码移到了函数下面,但我仍然遇到同样的问题。

最佳答案

这里有一个语法错误:

tl.loadJSON(jsonFile), function(json, url) {
                    ^ the parentheses is prematurely closed
    eventSource.loadJSON(json, url); 
});
 ^ syntax error, unexpected token

您必须删除 jsonFile 参数后的右括号:

tl.loadJSON(jsonFile, function(json, url) {
    eventSource.loadJSON(json, url);
});

这个 SyntaxError 破坏了函数声明,这就是为什么 onLoad 没有定义的原因。

关于javascript - 为什么找不到这个 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3073922/

相关文章:

javascript - 使用 JS/jQuery 启用/禁用 DOM 元素的事件

javascript - 从ajax返回的数据对象中读取多个变量

javascript - C3.js - 嵌套的 JSON 对象,如何访问和加载数据?

javascript - 围绕一个圆圈动态排列一些元素

jquery - 如何隐藏 jQuery 中具有特定值 "for"属性的 label 标签?

php - 悬停时隐藏 <a> 标签上的标题

javascript - 检测对象的属性值为空

javascript - 如何获取 "parentNode"中所有id以相同字符串开头的元素?

javascript - Sencha Touch 2.3.1 中消息框变得无响应

javascript - 元素的 src 属性替换,以便在加载时首先呈现修改后的元素