繁体   English   中英

获取全局变量以在函数中使用

[英]Getting global var to use in a function

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

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”,但是我无法获取current_id的值

有人可以帮我吗?

提前致谢!

不要使用var current_id;

直接使用window.current_id

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM