如何使用twitter bootstrap popover获取动态内容

ali*_*ith 5 jquery google-maps popover twitter-bootstrap

我想在popover上显示谷歌地图

但是popover无法改变地图

仅在修复地图时才有效

我的代码:

$(function(){

// getmap function from http://stackoverflow.com/questions/10545768/google-map-in-twitter-bootstrap-popver
var getMap = function(opts) {
  var src = "http://maps.googleapis.com/maps/api/staticmap?",
  params = $.extend({
    center: 'New York, NY',
    zoom: 14,
    size: '512x512',
    maptype: 'roadmap',
    sensor: false
  }, opts),
  query = [];

  $.each(params, function(k, v) {
query.push(k + '=' + encodeURIComponent(v));
  });

  src += query.join('&');
  return '<img src="' + src + '" />';
}



$('.map').click(function(){
    location = $(this).text();
    $("#map").html(getMap({center: location}));
    });


$('.map').popover({trigger:'click',placement:'left',html:true,content:function(){
    return $("#map").html();
    }});    

    });
Run Code Online (Sandbox Code Playgroud)

我有这个功能的问题:

$('.map').click(function(){...});
Run Code Online (Sandbox Code Playgroud)

单击链接(.map),因为#map已更改,弹出窗口已断开且无效

谢谢

fro*_*ier 17

我不认为你需要分两个步骤(点击然后弹出),除非有一些你没有提到的原因,否则你也不需要div.这对我有用:

JavaScript的:

$('.map').popover({trigger:'click',placement:'left',html:true,content:function(){
     return getMap({center: $(this).text()})
  }});    
});
Run Code Online (Sandbox Code Playgroud)

然后,您可以center: 'New York, NY',从getMap参数中删除该行.

HTML:

<a href="#" class="map">New York, NY</a>
Run Code Online (Sandbox Code Playgroud)

如果您使用#map div来使弹出窗口变大,只需改变弹出窗口的CSS,通过执行以下操作:

.popover { width: 512px; height: 512px; }
Run Code Online (Sandbox Code Playgroud)

我会建议使用更具体的选择器,否则它会影响您网站上其他地方的弹出窗口.