根据您的评论,这就是 TJ 所说的 - 您需要将第二个脚本块变成如下内容:
<script type="application/javascript" src="Resources/JavaScript/proj4js-combined.js"></script>
<script type="application/javascript">
function mouseMove(sender,eventArgs) {
var source = new Proj4js.Proj('EPSG:3116');
var dest = new Proj4js.Proj('WGS84');
var p = new Proj4js.Point(px, py);
Proj4js.transform(source, dest, p);
}
</script>
...但是您实际上应该将内联代码块(<script>
我的答案中的第二个标签内的内容)移动到外部 Javascript 文件中。
编辑 1:您的编程背景是什么?如果它是 C# 或 Java 之类的东西,您将需要忘记您对这些的了解并以完全不同的方式处理 Javascript。Javascript 是一种解释语言,而不是编译语言;除其他外,这意味着声明函数的顺序很重要。
当我说“函数声明”时,我指的是任何看起来像这样的东西:
function myNewFunction()
{
// anything else here
}
这告诉 Javascript 解释器一个新函数,称为myNewFunction
,它的主体由大括号中的任何内容组成。
下面是我所说的一个例子,当我说你在声明一个函数之前就在使用它。考虑以下代码块(与任何其他 Javascript 隔离,例如,在外部 Javascript 文件中):
function foo() // this line declares the function called "foo"
{
}
function bar() // this line declares the function called "bar"
{
foo(); // this line invokes the previously declared function called "foo"
}
这将按预期工作,因为foo()
在您调用它之前已声明。但是,您尝试做的(听起来)与此类似:
function foo() // this line declares the function called "foo"
{
bar(); // this line tries to invoke the function called "bar"
// but it hasn't been declared yet! so it's undefined
}
function bar() // this line declares the function called "bar"
{
}
如果您要运行第二个 Javascript 片段,它将无法工作,因为您在声明之前调用了一个函数。*
*脚注:实际情况并非如此,因为使用这种语法 ( function foo() { ... }
) 在 Javascript 中做了一些特殊而神奇的事情。我的特定示例实际上会起作用,但它说明了您遇到的问题。