【问题标题】:Do I have a OpenLayers / jQuery conflict here?我这里有 OpenLayers / jQuery 冲突吗?
【发布时间】:2012-09-01 00:00:38
【问题描述】:

这是我的控制器 js.coffee 文件中的咖啡脚本代码。我只是想打开一个以特定位置为中心的简单地图等。我的地图可以工作并且moveend_event 回调被触发,因为如果我取消注释alert("hello") 调用它工作正常。

$(document).ready ->

  map = undefined
  options =
    projection: new OpenLayers.Projection('EPSG:900913')
    displayProjection: new OpenLayers.Projection("EPSG:4326")
    center: new OpenLayers.LonLat(-115.540123, 54.073189)
    controls: [new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.LayerSwitcher()]
    eventListeners:
      'moveend': moveend_event
  map = new OpenLayers.Map("map_element", options)
  osm = new OpenLayers.Layer.OSM('OpenStreetMap Layer')
  map.addLayer osm
  map_center = new OpenLayers.LonLat(-115.540123, 54.073189).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject())
  map.setCenter(map_center, 11)

moveend_event = ->
  #alert "Hello!"
  #$(".map_location").innerHTML "Hello!"

我很接近,但我不能认为我可能有 Openlayers / jQuery 冲突。如果我尝试使用 $(".map_location").innerHTML "Hello!" 设置我的 map_location div,页面创建者和错误控制台显示:

TypeError: 'undefined' 不是函数(评估 '$(".map_location").innerHTML("Hello!")')

如果有帮助,我会在打开图层之前加载 jquery。我做了一些谷歌搜索,它可能是 jQuery noConflict 模式(http://api.jquery.com/jQuery.noConflict/),但我是一个 javascript 新手,我不知道这里的问题或如何解决它。我确信这很容易解决,但我目前不知道。

【问题讨论】:

    标签: jquery ruby-on-rails coffeescript openlayers


    【解决方案1】:

    innerHtml不是jQuery函数,使用$('selector').html(new html here);而是。

    html 是 jQuery 对应的 javascript 的 innerHtml

    【讨论】:

    • 我是正式的傻瓜 - 解决了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 2012-01-01
    • 2011-07-10
    相关资源
    最近更新 更多