Jquery 从外部.js文件访问函数

Jquery 从外部.js文件访问函数,jquery,cordova,mobile,camera,Jquery,Cordova,Mobile,Camera,这是我的HTML中的外部.js文件 HTML:li id=“getPhoto” 访问此功能的最佳方式是什么。不能让它工作 $(document).ready(function() { $('#getPhoto').click(function() { function getPhoto(source) { // Retrieve image file location from specified source navigator.ca

这是我的HTML中的外部.js文件

HTML:li id=“getPhoto”

访问此功能的最佳方式是什么。不能让它工作

$(document).ready(function() {

  $('#getPhoto').click(function() {
      function getPhoto(source) {
         // Retrieve image file location from specified source
            navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
            destinationType: destinationType.FILE_URI,
            sourceType: source });
       }
   });
});

这里没有发生任何事情。单击#getPhoto时,您只是声明
getPhoto()
函数,而不是调用它。这就是调用函数的方式

$('#getPhoto').click(function() {
   getPhoto(someVar);
});

function getPhoto(source) {
    // Retrieve image file location from specified source
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { 
       quality: 50, 
       destinationType: destinationType.FILE_URI,
       sourceType: source 
    });
}

这会让你走上正确的道路。。但看起来您可能已经超出了您的想象。

实际上,您只是在单击时声明函数。如果删除几行,可以使用以下内容:

$(document).ready(function() {
 $('#getPhoto').click(function() {
  // Retrieve image file location from specified source
  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
  destinationType: destinationType.FILE_URI,
  sourceType: source });
 });
});

您不会将源变量传入,因此需要找到其他方法将其传入函数。

谢谢您的评论,我必须尝试一下。谢谢您的评论,我在想这个问题,但不确定。我要试试这个。
$('#getPhoto').click(function() {
   getPhoto(someVar);
});

function getPhoto(source) {
    // Retrieve image file location from specified source
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { 
       quality: 50, 
       destinationType: destinationType.FILE_URI,
       sourceType: source 
    });
}