javascript 在传单弹出窗口中显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10575765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:19:12  来源:igfitidea点击:

Show Image in Leaflet Popup

javascriptajaxjsonleaflet

提问by Ryan Clark

I'm trying to get the weather icon to show in a map marker using wunderground's api, Leaflet and Cloudmade. I've got the text showing and a variable with the icon image, but I'm not sure how to get it to show. Here's my code:

我正在尝试使用 wunderground 的 api、Leaflet 和 Cloudmade 使天气图标显示在地图标记中。我已经显示了文本和带有图标图像的变量,但我不知道如何让它显示出来。这是我的代码:

jQuery(document).ready(function($) {
    $.ajax({
          url: "http://api.wunderground.com/api/cd48ac26fb540679/conditions/q/pws:KCASANFR128.json",
          dataType: "jsonp",
          success: function(parsed_json) {
              var location = parsed_json['current_observation']['observation_location']['city'];
              var temp_f = parsed_json['current_observation']['temp_f'];
              var icon = parsed_json['current_observation']['icon_url'];
              marker1.bindPopup("Current temperature in " +location+ " is: " + temp_f).openPopup();
        }
    });
});

I tried this with no success:

我试过这个没有成功:

marker1.bindPopup( <img src=icon> "Current temperature in " +location+ " is: " + temp_f).openPopup();

Any suggestions?

有什么建议?

回答by IanI

The marker's bindPopup method just takes HTML content as a string, so you'll need to surround your tags with quotes as well - something like

标记的 bindPopup 方法仅将 HTML 内容作为字符串,因此您还需要用引号将标签括起来 - 类似

marker1.bindPopup( "<img src=" + icon_url + "/> Current temperature in " + location + " is: " + temp_f)

ought to work for you.

应该为你工作。