English 中文(简体)
Javascript scope in the context of onLoad vs. 注
原标题:Javascript Scope in the context of onLoad vs. onReady

我是新奇,想理解我守则的内容。 我正在使用jQuery和httpa href=“http:// openlayers.org/“rel=“noestlow noreferer”>OpenLayers /a>进行一些网络测绘。

这里是第1号法典,在Ready职能范围内创建了我的地图变量:

$(document).ready(function(){
    var map = new.OpenLayers.Map( map , options);
    $.get(my_python.cgi_script which returns an html table of available layers)
    iii

我的理解是,我的地图变量具有地方范围。

Within that function I add some layers, generate controls, etc. All working and covered in the OpenLayers documentation. I am also using jQuery s get method to call a python.cgi script which dynamically generates a table of available layers. This all occurs within the above onReady.

我需要以动态生成的内容开展工作,并发现我需要把我的守则放在Load的职能中。 如果我把第二条编码锁定为“Ready”功能,则由于做事情的顺序,th子无法通过 j子获得。

$(document).ready(function(){
    var map = new.OpenLayers.Map( map , options);
    //More code to dynamically generate a list of available layers, stored in a table
    $.get(my_python.cgi_script which returns an html table of available layers)
    iii

$(body).onLoad(function(){
 $(img.thumbnail).bind( click , function(){
  var name = $(this).attr( layer_name_id )
  var layer = new OpenLayers.WMS(//all the stuff to add the layer, including name)
  map.addLayer( layer ) //map undefined due to scope
iii
iii

在上述图中,第二个变量没有界定。 我理解为什么(范围)并决定该地图必须是全球性的变量。 我随后尝试:

var map; //This is a global because it is defined outside of any functions?
$(document).ready(function(){
    map = new.OpenLayers.Map( map , options);
    //More code to dynamically generate a list of available layers, stored in a table
    $.get(my_python.cgi_script which returns an html table of available layers)
    iii

$(body).onLoad(function(){
 $(img.thumbnail).bind( click , function(){
  var name = $(this).attr( layer_name_id )
  var layer = new OpenLayers.WMS(//all the stuff to add the layer, including name)
  map.addLayer( layer )
iii
iii

地图变量尚未确定。 为什么? 我对全球工作有何误解?

我通过将Load职能放在Ready的法典编队内,能够找到一切工作。 我不肯定以下工作的原因:

$(document).onReady(function(){
    var map = new.OpenLayers.Map( map , options);
    //More code to dynamically generate a list of available layers, stored in a table
    $.get(my_python.cgi_script which returns an html table of available layers)

$(body).onLoad(function(){
 $(img.thumbnail).bind( click , function(){
  var name = $(this).attr( layer_name_id )
  var layer = new OpenLayers.WMS(//all the stuff to add the layer, including name)
  map.addLayer( layer )
  iii
iii   

iii

最佳回答

你们宣布全球可变地图正在发挥作用的《守则》。 但是,在Ready和Load没有办法。 更好地创造(文件)功能,用美元(文件)完成。 • 准备和全负荷功能。 使用闲置功能并记录。 随时可进行的活动。

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签