2012-06-02 56 views
3

我正在製作一個新的jQuery插件,用於在頁面上繪製線條。 的問題是,我想不通這是怎麼回事,當我運行該腳本,在Chrome中,它說:「Uncaught SyntaxError:Unexpected identifier」error in jquery

"Uncaught SyntaxError: Unexpected identifier" 

8日至最後一行(一個,上面寫着:$.line();)。

整個代碼:

(function($) { 

    $.fn.line = function(coordinates) { 

     var bothpoints; 

     $.fn.line.draw = function() { 

      var lineColor = $('#lineRegion').attr('lineColor'); 

      if (!lineColor) { 
       var colors = ['black', 'blue', 'green', 'red', 'yellow', 'purple', 'magenta', 'cyan']; 

       $('img.line').each(function() { 
        colors.splice(colors.indexOf($(this).css('color')), 1); 
       }); 

       var lineColor = colors[0]; 

      } 

      var data = $.line.data(), 
       width = Math.abs(data[0] - data[2]), 
       height = Math.abs(data[1] - data[3]); 
      if (data[0] >> data[2]) { 
       var lineleft = data[2]; 
      } else { 
       var lineleft = data[0]; 
      } 
      if (data[1] >> data[3]) { 
       var linetop = data[1]; 
      } else { 
       var linetop = data[3]; 
      } 

      $('body').append('<img class="line" src="/lines/line-'+lineColor+'.png" style="position:absolute;top:'+linetop+';left:'+lineleft+';width:'+width+'px;height:'+height+'px;color:'+lineColor+';"/>'); 
     } 

     $.fn.line.data = function(coords) { 
      if (coords.length == 2) { 
       if (! $('#line').data('point1')) { 
        $('#line').data('point1', {x: coords[0], y: coords[1]}); 
        bothpoints = false; 
       } else { 
        $('#line').data('point2', {x: coords[0], y: coords[1]}); 
        bothpoints = true; 
       } 
       $.line.draw(); 
      } else if (coords == 1) { 
       $('#line').data('point1', false); 
       bothpoints = false; 
      } else if (coords == 2) { 
       $('#line').data('point2', false); 
       bothpoints = false; 
      } else if (!coords) { 
       return [$('#line').data('point1').x, $('#line').data('point1').y, $('#line').data('point2').x, $('#line').data('point2').y]; 
      } 
     } 

     $.fn.line.point = function() { 
      if (!($.line.data().length == 4)) { 
       var _posY = posy, _posX = posx; 
       $('body').append('<div class="point" style="position:absolute; top:' + _posY + 'px; left:' + _posX + 'px;"></div>'); 
       $.line.data([_posX, _posY]); 
       if (bothpoints == true) { 
        $.line.draw(); 
       } 
      } 
     } 

     $.fn.line.unpoint = function() { 
      this.hide('fast'); 
      if ($.line.data()[0] == this.offset()['left'] && $.line.data[1] == this.offset()['top']) { 
       $.line.data(1); 
      } else { 
       $.line.data(2); 
      } 
      $.line.erase(); 
     } 


     $.fn.line.erase = function(total) { 
      if (total == true) { 
       $('img.line').hide('fast'); 
       $('div.point').hide('fast'); 
       $.line.data(1); 
       $.line.data(2); 
      } else { 
       $('img.line').hide('fast'); 
      } 
     } 

     if (coordinates.length == 4) { 
      $.line.data([coordinates[0], coordinates[1]]); 
      $.line.data([coordinates[2], coordinates[3]]); 
      $.line.draw(); 
     } 

    }; 

    var posx, posy; 

    $(document).ready(function(){ 
     $(document).on('mousemove', function(e) { 
      if (!e) var e = window.event; 
      if (e.pageX || e.pageY)  { 
       posx = e.pageX; 
       posy = e.pageY; 
      } 
      else if (e.clientX || e.clientY) { 
       posx = e.clientX + document.body.scrollLeft 
        + document.documentElement.scrollLeft; 
       posy = e.clientY + document.body.scrollTop 
        + document.documentElement.scrollTop; 
      } 
     } 
     $.line(); 
     $('head').append('<meta id="line" />'); 
     $('#lineRegion').click($.line.point()); 
     $('.point').click($.line.unpoint()); 
     $('.lineEraser').click($.line.erase(true)); 
    } 

})(jQuery); 
+2

要檢測這種簡單的編碼錯誤,請嘗試使用JSHint/JSLint。他們通常會提供更明智的錯誤消息,並使您擁抱更好的編碼實踐。 –

+0

@ssg - JSLint太嚴格IMO,「缺少分號」。 ':/' –

+1

@Derek JSLint的設置可以調整,而JSHint比JSLint更接近於地面。然而,「缺少分號」很重要,因爲JavaScript中的「自動分號插入」會導致嚴重的錯誤,而不會在丟失錯誤時引發錯誤。我強烈建議保持該選項。 –

回答

12

你啓動一個在線功能:$(document).on('mousemove', function(e) {和之後寫不完的聲明。在發生錯誤之前,您需要});而不僅僅是}

+1

有時候就這麼簡單。我不敢相信我忽略了這一點。非常感謝! – PitaJ

+0

現在它的最後一行是''Unexpected token)「'' – PitaJ

+1

看起來它與最後一行之前的行有同樣的問題。您需要關閉'document.ready'調用。 – TLS

相關問題