Javascript 获取要在函数中使用的全局变量

Javascript 获取要在函数中使用的全局变量,javascript,global-variables,Javascript,Global Variables,我正在使用此代码播放和暂停几个音频: jQuery(document).ready(function(){ var getaudio; var audiostatus = 'off'; var current_id; jQuery(document).on('click touchend', '.speaker', function() { elemento = jQuery(this); current_id = elemento.children("audio

我正在使用此代码播放和暂停几个音频:

jQuery(document).ready(function(){
  var getaudio;
  var audiostatus = 'off';
  var current_id;
  jQuery(document).on('click touchend', '.speaker', function() {
    elemento = jQuery(this);
    current_id = elemento.children("audio").attr("id");
    clase = current_id.replace(/player/, '');

    if (!jQuery('.c'+clase).hasClass("speakerplay")) {
       getaudio = jQuery('#'+current_id)[0];
       if (audiostatus == 'off') {
         jQuery('.c'+clase).addClass('speakerplay');
         getaudio.load();
         getaudio.play();
         audiostatus = 'on';
         return false;
     } else if (audiostatus == 'on') {
       jQuery('.c'+clase).addClass('speakerplay');
       getaudio.play()
     }
   } else if (jQuery('.speaker').hasClass("speakerplay")) {
     getaudio.pause();
     jQuery('.c'+clase).removeClass('speakerplay');
     audiostatus = 'on';
   }
  });

  // Here is my problem: I need to get the value of current_id...
  jQuery('#'+current_id).on('ended', function() {
    jQuery('.speaker').removeClass('speakerplay');
    audiostatus = 'off';
  });
});
在上一个函数中,我想在音频结束后删除类“speakerplay”,但无法获取当前\u id的值

有人能帮我吗

提前谢谢

不要使用 无功电流

直接使用window.current\u id

window.current_id = elemento.children("audio").attr("id");

jQuery('#'+window.current_id).on('ended', function() {

只需将处理程序添加到文档中,这样您就可以像在click处理程序中一样访问正确的元素thorugh
$(this)
?当前的_id甚至不必是全局的或变量,因为它只用于选择正确的元素。感谢Shilly的回复!但我很震惊,我恐怕不太明白你的意思。你能给我举个例子吗?谢谢Geo Paul的回答!我已经按照你说的那样试过了,但是还没有成功。。。如果我在控制台中写入'window.current_id',我会得到正确的值,但函数似乎不起作用。。。有什么想法吗?用window.current\u id替换“#”+window.current\u id