函数“未定义” [英] Function "is not defined"

查看:116
本文介绍了函数“未定义”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码

 < script type =text / javascriptsrc =http:// www。 google.com/jsapi\"></script> 
< script type =text / javascript>
google.load(maps,3,{other_params:sensor = false});
google.load(jquery,1.3.2);
google.load(visualization,1,{packages:[columnchart]});

函数initialize(){

//一些动作...

函数mapload(myfile){
jQuery.get trace_+ myfile +.xml,{},函数(data){
//一些动作...
});
}

mapload('ah');

}

google.setOnLoadCallback(initialize);
< / script>

< input type =buttonvalue =Hunt Mesaonclick =mapload('hunt')/>

第一个mapload正常工作

但是onclick按钮会显示mapload没有定义。



你知道为什么么?

解决方案

mapload()仅在 initialize()中定义。当 onclick 处理程序尝试调用它时,它不再存在。为了解决你的问题,一个快速和肮脏的解决方案是用 window.mapload 替换所有出现的 mapload 。所以写

  window.mapload = function(myfile){
jQuery.get(trace_+ myfile + .xml,{},函数(data){
//一些动作...
});
}

 < input type =buttonvalue =Hunt Mesaonclick =window.mapload('hunt')/> 


I have this code

        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
        google.load("maps", "3",  {other_params:"sensor=false"});
        google.load("jquery", "1.3.2");
        google.load("visualization", "1", {packages: ["columnchart"]});

        function initialize() {

            // some actions...

            function mapload(myfile) {
                    jQuery.get("trace_" + myfile + ".xml", {}, function(data) {
                        // some actions...
                    });
            }

            mapload('ah');

        }

        google.setOnLoadCallback(initialize);
        </script>

        <input type="button" value="Hunt Mesa" onclick="mapload('hunt')" />

The first "mapload" works fine

But the onclick button say "mapload is not defined".

Do you know why ?

解决方案

mapload() is only defined within initialize(). When your onclick handler tries to call it, it doesn't exist any more. To solve your problem, a quick&dirty solution is to replace all occurrences of mapload with window.mapload. So write

        window.mapload = function (myfile) {
                jQuery.get("trace_" + myfile + ".xml", {}, function(data) {
                    // some actions...
                });
        }

and

<input type="button" value="Hunt Mesa" onclick="window.mapload('hunt')" />

这篇关于函数“未定义”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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