火狐操作系统中图像元素上的点击事件


     function listContents(storagename) {
        alert("inside function");
        //Clear up the list first
            $('#results').html("");
            var files = navigator.getDeviceStorage(storagename);
            var cursor = files.enumerate();
            cursor.onsuccess = function () {
              //alert("Got something");
              var file = this.result;
              if (file != null) {
        var imageElement = $('<img height="100" width="75">');
              imageElement.attr('src', window.URL.createObjectURL(file));
              var tagvalue=  $("<p>" + file.name + "," + file.lastModifiedDate + "," + file.type + "," + file.size  + "</p>").appendTo('#results');
              imageElement.appendTo("#results");
                this.done = false;
              }
              else {
                this.done = true;
              }
              if (!this.done) {
                this.continue();
              }
            }
}
imageElement.onclick = function() { 
console.log('onclick function!');
//alert('blah');
}

我正在从火狐操作系统中的SDCard检索音频文件列表。现在我想将此文件上传到服务器,以便当我onclick图像元素时,我可以执行任何事件,所以我尝试显示警报框,但它不起作用。

在您的 sniplet 中imageElement在函数之外不可见listContents您需要通过绑定以其他方式注册 onclick 处理程序。

$('#results > img').on('click', function() { 
  console.log('onclick function!');
  //alert('blah');
});

或者在列表内容函数中注册它。

其次,如果你使用的是jQuery,坚持使用它,使用click()on()方法,而不是普通的javascript事件(在你的例子中onclick

作为提示:要清理元素内容,请使用空函数,如下所示:

$('#results').empty();

最新更新