Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery Javascript音频播放/停止_Jquery_Jquery Mobile - Fatal编程技术网

Jquery Javascript音频播放/停止

Jquery Javascript音频播放/停止,jquery,jquery-mobile,Jquery,Jquery Mobile,仍然有问题-如果我让我的代码播放选定的音频按钮将不会切换。如果我包含$(document).ready(function(){),则按钮会切换,但音频不会播放!有人能保存我留下的头发吗?这是一个在iOS 5上交付的HTML5 jQuery移动项目 谢谢你的帮助 J 代码: html: 选择一个频率 16Hz 18Hz 20Hz 好的,这里有一点假设,但我认为这是一个范围问题。您需要在document ready处理程序外部声明变量和函数,但在其内部附加按钮事件 var selSound = '

仍然有问题-如果我让我的代码播放选定的音频按钮将不会切换。如果我包含
$(document).ready(function(){
),则按钮会切换,但音频不会播放!有人能保存我留下的头发吗?这是一个在iOS 5上交付的HTML5 jQuery移动项目

谢谢你的帮助 J

代码:

html:


选择一个频率
16Hz
18Hz
20Hz

好的,这里有一点假设,但我认为这是一个范围问题。您需要在document ready处理程序外部声明变量和函数,但在其内部附加按钮事件

var selSound = '18000'; //default to first sound.
var play = false; //default to paused.

function selectSound(id) {
    selSound = id;
}

$(document).ready(function(){
    $('#playbtn').click(function() {
        var $this = $(this);
        var a = document.getElementById(selSound);   
        if ($this.text() == 'Zap!') {
            $this.children().children().text('Stop');
            a.play();
        } else {
            $this.children().children().text('Zap!');
            a.pause();
            alert('Stopped playing song: ' + selectedMP3);
        }
    });
})

好的,这里有一点假设,但我认为这是一个范围问题。您需要在document ready处理程序外部声明变量和函数,但在其中附加按钮事件

var selSound = '18000'; //default to first sound.
var play = false; //default to paused.

function selectSound(id) {
    selSound = id;
}

$(document).ready(function(){
    $('#playbtn').click(function() {
        var $this = $(this);
        var a = document.getElementById(selSound);   
        if ($this.text() == 'Zap!') {
            $this.children().children().text('Stop');
            a.play();
        } else {
            $this.children().children().text('Zap!');
            a.pause();
            alert('Stopped playing song: ' + selectedMP3);
        }
    });
})

您应该将
函数selectSound(id)
移到
$(文档)之外。ready(function(){})
我将通过更改id属性的值来开始解决您的问题。根据javascript的外观,您正在选择一个无效的id(selSound='18000')。还可以看到您应该移动
函数selectSound(id)
$(document).ready(function(){});
我将通过更改id属性的值来开始解决您的问题。根据javascript的外观,您正在选择一个id(selSound='18000')这是无效的。请注意,这不是范围问题。selSound和play都不在selectSound和click事件处理程序的范围内。实际上,我会保留问题中的javascript。我所说的范围是document ready事件。示例中声明了这两个变量,因此它们不全局存在。这就是我的同事所说的修复。非常感谢您的时间-太好了。-吉米不确定在文档准备就绪之外,selSound和play需要在哪里。我遗漏了什么吗?我们这里没有完整的脚本,就像我说的,这是根据他对问题的措辞所做的假设。这显然是一个范围问题。这不是范围问题。selSound和play都是在selectSound和click事件处理程序之外。我实际上会保留问题中的javascript。我所说的范围是document ready事件。示例中声明了两个变量,因此它们不存在于全局。这是我的代码修复的。非常感谢您花这么多时间-太好了。-JimI不确定在文件准备就绪之外需要声音和播放的地方。我遗漏了什么吗?我们这里没有完整的脚本,就像我说的,这是他对问题措辞的一种假设。这显然是一个范围问题。
var selSound = '18000'; //default to first sound.
var play = false; //default to paused.

function selectSound(id) {
    selSound = id;
}

$(document).ready(function(){
    $('#playbtn').click(function() {
        var $this = $(this);
        var a = document.getElementById(selSound);   
        if ($this.text() == 'Zap!') {
            $this.children().children().text('Stop');
            a.play();
        } else {
            $this.children().children().text('Zap!');
            a.pause();
            alert('Stopped playing song: ' + selectedMP3);
        }
    });
})