0

我在控制台中收到此错误

SyntaxError: Unexpected token '(' ... line 49

这是有问题的行:

 trackEnded: function() {

这是整个代码,我只是不明白为什么会出现该错误?

// Initialize audio js
      $(document).ready(function($) {


              // Setup the player to autoplay the next track
              trackEnded: function() {
                  var next = $('.main article.playing').next();
                  if (!next.length) next = $('.main article').first();
                  next.addClass('playing').siblings().removeClass('playing');
                  audio.load($('.article-play', next).attr('data-src'));
                  audio.play();
                  $('.article-play', next).removeAttr('data-playa').attr("data-pausea", '5');
                  var nextPlay = $('.article-play', next);
                  $('.article-play').not(nextPlay).removeAttr('data-pausea').attr("data-playa", '4');
              }


          //Synchronize Main player with articles
          $(".audiojs .play-pause").click(function() {
              var element = $('.playing .article-play')
              var play = element.attr('data-playa')
              var pause = element.attr('data-pausea')
              if (play == '4') {
                  element.removeAttr('data-playa').attr("data-pausea", '5');
              } else if (pause == '5') {
                  element.removeAttr('data-pausea').attr("data-playa", '4');
              }
          })

          // Load in a track on click
          $('.article-play').click(function(e) {
              e.preventDefault();
              if ($(this).closest('article').hasClass('playing')) {
                  var play = $(this).attr('data-playa')
                  var pause = $(this).attr('data-pausea')
                  if (play == '4') {
                      $(this).removeAttr('data-playa').attr("data-pausea", '5');
                      $('.article-play').not($(this)).removeAttr('data-pausea').attr("data-playa", '4');
                      audio.play();
                  } else if (pause == '5') {
                      $(this).removeAttr('data-pausea').attr("data-playa", '4');
                      audio.pause();
                  }
              } else {
                  $(this).closest('article').addClass('playing').siblings().removeClass('playing');
                  audio.load($(this).attr('data-src'));
                  audio.play();
                  $(this).removeAttr('data-playa').attr("data-pausea", '5');
                  $('.article-play').not($(this)).removeAttr('data-pausea').attr("data-playa", '4');
              }
          });

          // Keyboard shortcuts
          $(document).keydown(function(e) {
              var unicode = e.charCode ? e.charCode : e.keyCode;
              // right arrow
              if (unicode == 39) {
                  var next = $('.main article.playing').next();
                  if (!next.length) next = $('.main article').first();
                  next.find('.article-play').click();
                  // back arrow
              } else if (unicode == 37) {
                  var prev = $('.main article.playing').prev();
                  if (!prev.length) prev = $('.main article').last();
                  prev.find('.article-play').click();
                  // spacebar
              } else if (unicode == 32 && document.activeElement.nodeName !== 'INPUT') {
                  audio.playPause();
                  var element = $('.playing .article-play')
                  var play = element.attr('data-playa')
                  var pause = element.attr('data-pausea')
                  if (play == '4') {
                      element.removeAttr('data-playa').attr("data-pausea", '5');
                  } else if (pause == '5') {
                      element.removeAttr('data-pausea').attr("data-playa", '4');
                  }
              }
          });
      });
4

3 回答 3

2

您正在尝试在函数范围内编写对象语法,这是不正确的语法。

应该:

function trackEnded() {
    // rest or your code
于 2013-06-11T14:32:44.510 回答
1

问题是

$(document).ready(function($) {
    // Setup the player to autoplay the next track
    trackEnded: function() {
        ...
    }
    ...
)};

语法不正确

您可能希望这样做:

$(document).ready(function($) {
    // Setup the player to autoplay the next track
    function trackEnded() {
        ...
    }
    ...
)};

此外,查看您的表格和评论,您的预期代码似乎类似于:

$(document).ready(function($) {
    // Setup the player to autoplay the next track
    $(element).AudioPlugin({
        trackEnded: function() {
            ...
        }
    });
    ...
)};

不同之处在于,对于假定的 AudioPlugin,我传递了一个初始化/选项对象,该对象定义了一个函数回调trackEnded

于 2013-06-11T14:33:13.573 回答
0

这是您的代码的简化,它仍然会重现该问题:

function(){
    trackEnded: false
}

总结一下:您正在尝试定义一个对象键,但您没有对象文字。

于 2013-06-11T14:33:39.060 回答