我有一些应该在 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/