【问题标题】:how to use twitter bootstrap popover for dynamic content如何使用 twitter bootstrap popover 获取动态内容
【发布时间】:2013-03-15 16:21:28
【问题描述】:

我想在弹出框上显示谷歌地图

但是弹窗改变地图失败

仅在地图固定时有效

我的代码:

$(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();
    }});    

    });

我对这个功能的问题:

$('.map').click(function(){...});

点击链接(.map)因为#map被改变,弹出框被破坏并且不起作用

谢谢

【问题讨论】:

    标签: jquery google-maps twitter-bootstrap popover


    【解决方案1】:

    我认为您不需要分两个单独的步骤执行此操作(单击然后弹出窗口),也不需要 div,除非有一些您没有提到的原因。这对我有用:

    JavaScript:

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

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

    HTML:

    <a href="#" class="map">New York, NY</a>
    

    如果您使用#map div 使弹出框更大,只需更改弹出框的 CSS,方法如下:

    .popover { width: 512px; height: 512px; }
    

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

    【讨论】:

    • 我已经尝试过这种方法:`$('.map').popover({...,content:function(){ return getMap({center: $(this).text()}) }}); }); 但只有在地图固定时才有效,getMap({center: $(this).text()}) 表示内容会随着每次点击
    • 这段代码运行良好,位置变量的问题!!
    • 是的,位置不是一个与 JavaScript 一起使用的好变量,因为某些浏览器会将其解释为您想要访问该 URL。不过,我很高兴你让它工作了。
    • 这个答案对我不起作用,但确实如此:stackoverflow.com/a/19684440/482115
    猜你喜欢
    • 2014-05-28
    • 1970-01-01
    • 2014-04-08
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 2012-02-29
    相关资源
    最近更新 更多