Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.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
Javascript不使用=运算符执行函数_Javascript_Html_Dom - Fatal编程技术网

Javascript不使用=运算符执行函数

Javascript不使用=运算符执行函数,javascript,html,dom,Javascript,Html,Dom,我是javascript新手,所以这是一个基本问题。我创建了一个简单的mp3播放器,它加载第一首歌曲,然后以数组形式播放接下来的几首歌曲。我修改了在堆栈上找到的代码,它可以工作: audioPlayer.onended = function() { if(currentSong < nextSong.length-1) { audioPlayer.src = nextSong[++currentSong]; document.getElementBy

我是javascript新手,所以这是一个基本问题。我创建了一个简单的mp3播放器,它加载第一首歌曲,然后以数组形式播放接下来的几首歌曲。我修改了在堆栈上找到的代码,它可以工作:

audioPlayer.onended = function() {
    if(currentSong < nextSong.length-1) {
        audioPlayer.src = nextSong[++currentSong];
        document.getElementById('songTitle').innerHTML 
                                = songTitle[currentSong];
    }
}
audioPlayer.onedded=function(){
如果(当前歌曲
但是,如果我尝试将实现放在其自己的函数中,并以这种方式调用该函数,则该函数不起作用:

audioPlayer.onended = nextSong();

function nextSong() {
    if(currentSong < nextSong.length-1) {
        audioPlayer.src = nextSong[++currentSong];
        document.getElementById('songTitle').innerHTML 
                                = songTitle[currentSong];
    }
}
audioPlayer.onended=nextSong();
函数nextSong(){
如果(当前歌曲

我不想每次使用函数nextSong()时都重写代码。例如,我尝试从标记内的按钮调用nextSong()函数,但无法调用该函数。谢谢你的帮助。

这是一个常见的困惑。第二个示例实际执行的是运行
nextSong
函数,并将其返回值分配给
onended

相反,您可以将代码更改为:

function nextSong() {
    if(currentSong < nextSong.length-1) {
        audioPlayer.src = nextSong[++currentSong];
        document.getElementById('songTitle').innerHTML 
                                = songTitle[currentSong];
    }
}

// Assign the function (nextSong) not its return value (nextSong())
audioPlayer.onended = nextSong;
函数nextSong(){
如果(当前歌曲
这是一种常见的混淆。第二个示例实际执行的是运行
nextSong
函数,并将其返回值分配给
onended

相反,您可以将代码更改为:

function nextSong() {
    if(currentSong < nextSong.length-1) {
        audioPlayer.src = nextSong[++currentSong];
        document.getElementById('songTitle').innerHTML 
                                = songTitle[currentSong];
    }
}

// Assign the function (nextSong) not its return value (nextSong())
audioPlayer.onended = nextSong;
函数nextSong(){
如果(当前歌曲