Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Błąd w moim kodzie - missing : after property id
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
finito
Ślęczę nad tym i ślęczę i nie mogę znaleźć tej literówki. W okolicy 31...32 jest błąd...To musi być jakaś moja literów, bo nawet nie wiem gdzie miałbym zapomnieć o ":". Pomożecie? W znalezieniu? Nie wiem. Gdzieś nawiasu nie dopisałem?

  1. (function($){
  2. $.fn.changeColor = function(options)
  3. {
  4.  
  5. function onClick(event){
  6. //cialo metody prywatnej
  7. $this = $(this);
  8. data = $this.data('changeColor');
  9. data.first = !data.first;
  10. $this.css("backgroundColor", data.first ? data.colorSecond : data.colorFirst);
  11. return true;
  12. }
  13. //metody publiczne
  14. var methods = {
  15. swapColors: function(){
  16. $this=$(this);
  17. data = $this.data('changeColor');
  18. tmp = data.colorSecond;
  19. data.colorSecond = data.colorFirst;
  20. data.colorFirst = tmp;
  21. },
  22. destroy: function(){
  23. //destruktor
  24. $this = $(this);
  25. $this.unbind("click");
  26. $this.css("backgroundColor","");
  27. $this.removeData("changeColor");
  28. }
  29. };
  30. return this.each(function()({
  31. if(methods[options])
  32. {
  33. return methods[options].apply(this,arguments);
  34. }
  35. else if (typeof options === ' object ' || ! options){
  36. //parametry pluginu
  37. var settings = $.extend({
  38. colorFirst:'Red',
  39. colorSecond:'Green'
  40. },options);
  41.  
  42. $this = $(this);
  43. data = $this.data('changeColor');
  44. if (!data)
  45. {
  46. data = $this.data('changeColor',settings);
  47. $this.bind('click',onClick);
  48. }
  49. $this.css("background-color",settings.colorFirst);
  50. data.first=true;
  51. return;
  52. }
  53.  
  54. else
  55. {
  56. //bład
  57. $.error('changeColor: no method: '+ options);
  58. }
  59.  
  60. });
  61.  
  62. }
  63.  
  64. })(jQuery);
trueblue
  1. return this.each(function(){


finito
Cytat(trueblue @ 10.05.2014, 16:49:05 ) *
  1. return this.each(function(){


Dziękować - potforna literówka.

Tyle, że teraz wyskakuje mi błąd - nie wiem czemu.
rror: changeColor: no method: [object Object] i wskazuje mi na linię 247 jquery.js

************
A zabawne, że oryginał działa;-)) Pewnie znowu jakaś literówka.
**********
Mam wrażenie, że nie jasno opisałem mój problem. Może dlatego, że sam nie bardzo wiem na czym on polega.

Porównałem wszystkie pliki i wrzysko jest tak samo. Tyle, że oryginał działa a moje wersje nie. I tyle. Będę wdzięczny za jakieś sugestie.

Okej, dla jasności wrzucam wszystkie kody. Tak będzie chyba najłatwiej znaleźć błąd.
Kod html:
  1. <!DOCTYPE html>
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  3. <title>Chmurkowa Strona</title>
  4. <link rel="icon" type="image/png" href="favicon.png"/>
  5. <link rel="stylesheet" href="style.css">
  6. <script type=""src="jquery.js"></script>
  7. <script src="changeColor.js"></script>
  8. <script type="text/javascript">
  9. $(document).ready(function(){
  10. $('div').changeColor({colorFirst:'Blue',colorSecond:'Black'});
  11. });
  12. </script>
  13. </head>
  14. <div id="all">
  15.  
  16. </div>
  17.  
  18. </body>
  19. </html>

Kod js:
  1. (function($){
  2. $.fn.changeColor = function(options)
  3. {
  4. //cialo metody prywatnej
  5. function onClick(event){
  6. $this = $(this);
  7. data = $this.data('changeColor');
  8. data.first = !data.first;
  9. $this.css("backgroundColor", data.first ? data.colorSecond : data.colorFirst);
  10. return true;
  11. }
  12. //metody publiczne
  13. var methods = {
  14. swapColors: function(){
  15. $this=$(this);
  16. data = $this.data('changeColor');
  17. tmp = data.colorSecond;
  18. data.colorSecond = data.colorFirst;
  19. data.colorFirst = tmp;
  20. },
  21. destroy: function(){
  22. //destruktor
  23. $this = $(this);
  24. $this.unbind("click");
  25. $this.css("backgroundColor","");
  26. $this.removeData("changeColor");
  27. }
  28. };
  29. return this.each(function(){
  30. if(methods[options])
  31. {
  32. return methods[options].apply(this,arguments);
  33. }
  34. else if (typeof options === ' object ' || ! options){
  35. //parametry pluginu
  36. var settings = $.extend({
  37. colorFirst:'Red',
  38. colorSecond:'Green'
  39. },options);
  40.  
  41. $this = $(this);
  42. data = $this.data('changeColor');
  43. if (!data)
  44. {
  45. data = $this.data('changeColor',settings);
  46. $this.bind('click',onClick);
  47. }
  48. $this.css("background-color",settings.colorFirst);
  49. data.first=true;
  50. return;
  51. }
  52.  
  53. else
  54. {
  55. //bład
  56. $.error('changeColor: no method: '+ options);
  57. }
  58.  
  59. });
  60.  
  61. }
  62.  
  63. })(jQuery);


Hura!! Znalazłem błąd. Chodziło o to, że jak jest if (typeof options === 'object'),to nie można pisać: ' object '. Musi być brak spacji. Wtedy działa. Mam nadzieję, że komuś się to spostrzeżenie przyda.

  1. return this.each(function(){
  2. if(methods[options])
  3. {
  4. return methods[options].apply(this,arguments);
  5. }
  6. else if (typeof options === 'object' || ! options){
  7. //parametry pluginu
  8. var settings = $.extend({
  9. colorFirst:'Red',
  10. colorSecond:'Green'
  11. },options);
  12.  
  13. $this = $(this);
  14. data = $this.data('changeColor');
  15. if (!data)
  16. {
  17. data = $this.data('changeColor',settings);
  18. $this.bind("click",onClick);
  19. }
  20. $this.css("background-color",settings.colorFirst);
  21. data.first=true;
  22. return;
  23. }
  24. else{
  25. //bład
  26. $.error('changeColor: no method: '+ options);
  27. }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.