2011-11-16 80 views
0

爲什麼我不能把下面的函數內部jQuery(document).ready(function() { }爲什麼我不能把我的功能jQuery的(文件)。就緒內(函數(){} t

function csf_map_maker_js(args) { 

      // Default js args.  Args passed to function will override these. 
      var default_args = { 
     width : 610, 
     height : 400, 
        latitude :  37.93, 
        longitude :  -75.09, 
        zoom : 8, 
     mtype: 'ROADMAP', 
     encoded_points: ' blah blah', 
     id: 'csf_map_canvas', 
     hood: 'My Neighborhood' 
      }; 


      for (var index in default_args) { 
        if (typeof args[index] == "undefined") { 
          args[index] = default_args[index]; 

        } 
      } 


     var latlng = new google.maps.LatLng(args['latitude'], args['longitude']); 
    var encodedPoints = args['encoded_points']; 
    var decodedPoints = google.maps.geometry.encoding.decodePath(encodedPoints); 

     var myOptions = { 
        zoom: parseInt(args['zoom']), 
        center: latlng, 
        mapTypeId: google.maps.MapTypeId[args['mtype']], 
     streetViewControl: true, 
     zoomControl: true, 
     panControl: true 
      }; 


    csf_map = new google.maps.Map(document.getElementById(args['csf_id']), myOptions); 

    var encodedPolygon = new google.maps.Polyline ({ 
     strokeColor: "#0000FF", 
     strokeOpacity: 0.55, 
     strokeWeight: 5, 
     path: decodedPoints, 
     fillOpacity: 0, 
     clickable: false, 
     map: csf_map 
    }); 
} 

如果我把它放在外面, (文檔).ready塊它的工作原理(儘管我認爲其他一些函數在映射加載之前被調用)代碼是用於WordPress插件

函數在php腳本中被調用如下:

$csf_map_output .= '<div id="csf_map_canvas" style="width:'. $atts['width'].'px; height: '. $atts['height'] .'px;"></div>'; 

$csf_map_output .= '<script>var csf_map_params = ' . json_encode($atts) . '; csf_map_maker_js(csf_map_params);</script>'; 

return $csf_map_output; 

當我在php腳本中調用函數的方式時,是否需要改變它的方式jQuery(document).ready(function() { }

+0

此代碼似乎缺少結尾'}' - 是你的問題嗎? – Hogan

+0

嘗試$(document).ready(function(){}); – stat

+0

謝謝你的留言。我得到了結局);在我的代碼中,但我沒有將其包含在我的問題中。 $與編寫jQuery相同。 – Laxmidi

回答

2

,如果你把你的函數在jQuery(document).ready(function() { });和你從你的PHP函數的調用不在jQuery(document).ready(function() { });所以它不會在那一刻

左右,但無論是在存在於您的函數創建函數調用之前, jQuery(document).ready(function() { });並確保你的函數在被調用之前被聲明。

+0

Hi @Frederick Behrends,謝謝你的留言。我必須從PHP腳本中調用該函數。如何讓php腳本調用(document).ready(function(){})中的函數; ?對不起,我很困惑。 – Laxmidi

+2

'$ csf_map_output。=''; ' –

+0

謝謝你的幫助! – Laxmidi

2

函數聲明的範圍是它在裏面定義的任何函數。

如果你在一個匿名函數中做一個函數聲明(把它傳遞給document.ready沒關係),那麼它不能被匿名函數以外的代碼調用。

+0

非常感謝您的評論。據我瞭解,我必須保持匿名函數以外的功能。在這種情況下,如何在加載所有內容後從php腳本調用函數?謝謝。 – Laxmidi

+0

把它放在onload而不是onready。 – Quentin

相關問題