空間首頁 | 博客 | 好友分享 | 相冊 | 存檔 | 朋友和群組 | 個人資料
iSocceriRun
 

rainfly

 
文章分類
 
缺省  
 
標題搜索
 
 
 
Our Sponsors
 
 
快速導航
 
首頁
論壇
Classified Search Engine
黃頁/二手
北美個人空間
免費注冊
登錄
 
統計
 
點擊: 210902
帖子數量: 215
開辟個人空間: 2011-08-11
最後更新: 2017-03-23
 
RSS訂閱
 
 
 
 
 
 
 
 

陸壓等專家請進,再請教一個jquery語法問題

文章內容
  2012-09-06 11:21:32  
下面代碼中,draw()這個函數反復出現(內容完全相同).
怎樣只用寫一次就可以復用?

代碼:

                $("input.knob_snd_aec").knob({
               change : function (value) {
                  if (current_id === 'snd_latency') {
                     $('#snd_latency').val(value);
                  }
                  if (current_id === 'snd_suppression') {
                     $('#snd_suppression').val(value);
                  }   
               },
               
               draw : function () {
                  if(this.$.data('skin') == 'tron') {
                     var a = this.angle(this.cv)        // Angle
                        , sa = this.startAngle        // Previous start angle
                        , sat = this.startAngle       // Start angle
                        , ea                     // Previous end angle
                        , eat = sat + a             // End angle
                        , r = 1;

                     this.g.lineWidth = this.lineWidth;

                     this.o.cursor
                        && (sat = eat - 0.3)
                        && (eat = eat + 0.3);

                     if (this.o.displayPrevious) {
                        ea = this.startAngle + this.angle(this.v);
                        this.o.cursor
                           && (sa = ea - 0.3)
                           && (ea = ea + 0.3);
                        this.g.beginPath();
                        this.g.strokeStyle = this.pColor;
                        this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, sa, ea, false);
                        this.g.stroke();
                     }

                     this.g.beginPath();
                     this.g.strokeStyle = r ? this.o.fgColor : this.fgColor ;
                     this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, sat, eat, false);
                     this.g.stroke();

                     this.g.lineWidth = 2;
                     this.g.beginPath();
                     this.g.strokeStyle = this.o.fgColor;
                     this.g.arc( this.xy, this.xy, this.radius - this.lineWidth + 1 + this.lineWidth * 2 / 3, 0, 2 * Math.PI, false);
                     this.g.stroke();

                     return false;
                  }
               }
            });
            $("input.knob_snd_nr").knob({
               change : function (value) {
                  if (current_id === 'snd_enr') {
                     $('#snd_enr').val(value);
                  }   
                  if (current_id === 'snd_isr') {
                     $('#snd_isr').val(value);
                  }   
               },
               draw : function () {
                  if(this.$.data('skin') == 'tron') {
                     var a = this.angle(this.cv)        // Angle
                        , sa = this.startAngle        // Previous start angle
                        , sat = this.startAngle       // Start angle
                        , ea                     // Previous end angle
                        , eat = sat + a             // End angle
                        , r = 1;

                     this.g.lineWidth = this.lineWidth;

                     this.o.cursor
                        && (sat = eat - 0.3)
                        && (eat = eat + 0.3);

                     if (this.o.displayPrevious) {
                        ea = this.startAngle + this.angle(this.v);
                        this.o.cursor
                           && (sa = ea - 0.3)
                           && (ea = ea + 0.3);
                        this.g.beginPath();
                        this.g.strokeStyle = this.pColor;
                        this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, sa, ea, false);
                        this.g.stroke();
                     }

                     this.g.beginPath();
                     this.g.strokeStyle = r ? this.o.fgColor : this.fgColor ;
                     this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, sat, eat, false);
                     this.g.stroke();

                     this.g.lineWidth = 2;
                     this.g.beginPath();
                     this.g.strokeStyle = this.o.fgColor;
                     this.g.arc( this.xy, this.xy, this.radius - this.lineWidth + 1 + this.lineWidth * 2 / 3, 0, 2 * Math.PI, false);
                     this.g.stroke();

                     return false;
                  }
               }
            });
            $("input.knob_snd_wndb").knob({
               change : function (value) {
                  if (current_id === 'snd_wndb_agr_val') {
                     $('#snd_wndb_agr_val').val(value);
                  }   
               },
               draw : function () {
                  ...//identical code here
               }
            });      
            $("input.knob_snd_mixer").knob({
               change : function (value) {
                  if (current_id === 'snd_mixer_postfilt_attn') {
                     $('#snd_mixer_postfilt_attn').val(value);
                  }   
                  if (current_id === 'snd_mixer_oar_maxrej') {
                     $('#snd_mixer_oar_maxrej').val(value);
                  }   
                  if (current_id === 'snd_mixer_oar_focus') {
                     $('#snd_mixer_oar_focus').val(value);
                  }   
               },
               draw : function () {
                  ...//identical code here
               }
            });      
               
∽rainfly∽ 點擊: 0 | 評論: 2 | 分類: 缺省 | 論壇: 電子電玩 | 論壇帖子
 
QR Code
請用微信 掃一掃 掃描上面的二維碼,然後點擊頁面右上角的 ... 圖標,然後點擊 發送給朋友分享到朋友圈,謝謝!
分享:
分享到微信

文章評論

陸壓
無題
代碼:


function myDrawFunction() {
                  if(this.$.data('skin') == 'tron') {
                     var a = this.angle(this.cv)        // Angle
                        , sa = this.startAngle        // Previous start angle
                        , sat = this.startAngle       // Start angle
                        , ea                     // Previous end angle
                        , eat = sat + a             // End angle
                        , r = 1;

                     this.g.lineWidth = this.lineWidth;

                     this.o.cursor
                        && (sat = eat - 0.3)
                        && (eat = eat + 0.3);

                     if (this.o.displayPrevious) {
                        ea = this.startAngle + this.angle(this.v);
                        this.o.cursor
                           && (sa = ea - 0.3)
                           && (ea = ea + 0.3);
                        this.g.beginPath();
                        this.g.strokeStyle = this.pColor;
                        this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, sa, ea, false);
                        this.g.stroke();
                     }

                     this.g.beginPath();
                     this.g.strokeStyle = r ? this.o.fgColor : this.fgColor ;
                     this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, sat, eat, false);
                     this.g.stroke();

                     this.g.lineWidth = 2;
                     this.g.beginPath();
                     this.g.strokeStyle = this.o.fgColor;
                     this.g.arc( this.xy, this.xy, this.radius - this.lineWidth + 1 + this.lineWidth * 2 / 3, 0, 2 * Math.PI, false);
                     this.g.stroke();

                     return false;
                  }
//======================華麗的分割線==========================
          $("input.knob_snd_aec").knob({
               change : function (value) {
                  if (current_id === 'snd_latency') {
                     $('#snd_latency').val(value);
                  }
                  if (current_id === 'snd_suppression') {
                     $('#snd_suppression').val(value);
                  }   
               },
               
               draw : myDrawFunction;
               }
            });
            $("input.knob_snd_nr").knob({
               change : function (value) {
                  if (current_id === 'snd_enr') {
                     $('#snd_enr').val(value);
                  }   
                  if (current_id === 'snd_isr') {
                     $('#snd_isr').val(value);
                  }   
               },
               draw : myDrawfunction ;
               }
            });
            $("input.knob_snd_wndb").knob({
               change : function (value) {
                  if (current_id === 'snd_wndb_agr_val') {
                     $('#snd_wndb_agr_val').val(value);
                  }   
               },
               draw : myDrawfunction ;
            });     
            $("input.knob_snd_mixer").knob({
               change : function (value) {
                  if (current_id === 'snd_mixer_postfilt_attn') {
                     $('#snd_mixer_postfilt_attn').val(value);
                  }   
                  if (current_id === 'snd_mixer_oar_maxrej') {
                     $('#snd_mixer_oar_maxrej').val(value);
                  }   
                  if (current_id === 'snd_mixer_oar_focus') {
                     $('#snd_mixer_oar_focus').val(value);
                  }   
               },
               draw : function () {
                  ...//identical code here
               }
            });     


is that what you want?

2012-09-06 11:44:59 | 引用
無題
陸壓 寫道:
[code]
is that what you want?

非常感謝.
但是運行時有問題--和原始代碼的結果有所不同
(可能因為我沒有給出全部代碼,誤導所致)

The code was from github.com/aterrien/jQuery-Knob

我換了種寫法,最終解決了問題,
代碼如下:

代碼:

            $(".knob").knob({
               draw : function () {
                  if(this.$.data('skin') == 'tron') {
                     var a = this.angle(this.cv)  // Angle
                        , sa = this.startAngle        // Previous start angle
                        , sat = this.startAngle       // Start angle
                        , ea                     // Previous end angle
                        , eat = sat + a             // End angle
                        , r = 1;

                     this.g.lineWidth = this.lineWidth;

                     this.o.cursor
                        && (sat = eat - 0.3)
                        && (eat = eat + 0.3);

                     if (this.o.displayPrevious) {
                        ea = this.startAngle + this.angle(this.v);
                        this.o.cursor
                           && (sa = ea - 0.3)
                           && (ea = ea + 0.3);
                        this.g.beginPath();
                        this.g.strokeStyle = this.pColor;
                        this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, sa, ea, false);
                        this.g.stroke();
                     }

                     this.g.beginPath();
                     this.g.strokeStyle = r ? this.o.fgColor : this.fgColor ;
                     this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, sat, eat, false);
                     this.g.stroke();

                     this.g.lineWidth = 2;
                     this.g.beginPath();
                     this.g.strokeStyle = this.o.fgColor;
                     this.g.arc( this.xy, this.xy, this.radius - this.lineWidth + 1 + this.lineWidth * 2 / 3, 0, 2 * Math.PI, false);
                     this.g.stroke();

                     return false;
                  }
               }
            });      
            $("input.knob_snd_aec").knob({
               change : function (value) {
                  if (current_id === 'snd_latency') {
                     $('#snd_latency').val(value);
                  }
                  if (current_id === 'snd_suppression') {
                     $('#snd_suppression').val(value);
                  }   
               }
            });
            $("input.knob_snd_nr").knob({
               change : function (value) {
                  if (current_id === 'snd_enr') {
                     $('#snd_enr').val(value);
                  }   
                  if (current_id === 'snd_isr') {
                     $('#snd_isr').val(value);
                  }   
               }
            });
            ...
            ...
            ...

2012-09-06 12:50:22 | 引用
rainfly

發表評論

The images, logos, trademarks used on this site and all forwarded content are the property of their respective owners.
We are not responsible for comments posted by our visitors, as they are the property of the poster.
All other content of this website is copyrighted by 加西網

 

加西網為北美中文網傳媒集團旗下網站