javascript Javascript按钮点击功能未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15637138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:29:23  来源:igfitidea点击:

Javascript button onclick function undefined

javascriptjqueryhtmlfunctioncordova

提问by DownTwoEarth

im currently using PhoneGap and for some reason when i click a button, the function is undefined. iv tryed putting it inside the deviceReady function, as well as the document ready function, but neither seemed to make a difference.

我目前正在使用 PhoneGap,出于某种原因,当我单击一个按钮时,该功能未定义。iv 尝试将其放入 deviceReady 函数以及文档就绪函数中,但似乎两者都没有区别。

I can't see what the problem is, if anyone can help that would be great.

我看不出问题是什么,如果有人可以提供帮助,那就太好了。

My HTML:

我的 HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.3.0.min.css" />
        <link rel="stylesheet" type="text/css" href="css/jquery.mobile.structure-1.3.0.min.css" />
        <link rel="stylesheet" type="text/css" href="css/jquery.mobile.theme-1.3.0.min.css" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <script type="text/javascript" src="cordova-2.5.0.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="phonegap.js"></script>
        <script type="text/javascript" src="js/libary.js"></script>
        <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.3.0.min.js"></script>
      <title>Libary</title>
    </head>
    <body>
      <div id="capture" class="wrapper" data-role="page">
        <div data-role="header">
          <h1>Libary</h1>
        </div>
        <div data-role="content" class="app">
          <a href="#" data-role="button" onclick="retrievePicture();">Retrieve Photo</a>
          <p id="pictureStatus"></p>
          <div id="imagelocation">
            <img id="capturedImage"></img>
          </div>
        </div>
      </div>
    </body>
</html>

My JavaScript/jQuery:

我的 JavaScript/jQuery:

document.addEventListener("deviceready", onDeviceReady, false);
var platform;

function onDeviceReady() {
    //Find out what platform the device is running
    platform = device.platform;

    //Camera Section
    function retrievePicture() {
          // Take a picture using device camera and retrieve image
          //navigator.camera.getPicture(onCaptureSuccess, onCaptureFail, {Camera.sourceType = Camera.PictureSourceType.PHOTOLIBRARY});
            $('#pictureStatus').text("hello");
    }

    function onCaptureSuccess(imageData) {
        $('#capturedImage').css("display", "block");
        var imageLocation = imageData;
        $('#capturedImage').attr("src", imageLocation);
        $('#pictureStatus').text(imageLocation);
    }

    function onCaptureFail(message) {
        $('#pictureStatus').text(message);
    }
}

回答by dsgriffin

Please don't define functions inside functions like that. Remove your retrievePicture()from inside the onDeviceReady()function and it should work fine.

请不要在这样的函数中定义函数。retrievePicture()onDeviceReady()函数内部删除你的,它应该可以正常工作。

As mentioned by @Robin van Baalen, this may be an interesting and relevant read - What you need to know about Javascript scoping.

正如@Robin van Baalen 所提到的,这可能是一个有趣且相关的阅读 -您需要了解的有关 Javascript 范围的内容

Also, Functions and function scope.

此外,函数和函数作用域