0

コンピューターに気象条件のアイコンがあります

アイコンごとにデフォルトのアイコンを変更したいのですが、どうすればよいですか

jsonファイルには次のような変数があります: "icon": "partlycloudy"

<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<body>
<img id='wicon'><img>
<script>
jQuery(document).ready(function($) {
 var state = 'CA';
 var city = 'San_Francisco';
 var URL = 'http://api.wunderground.com/api/102376e7c0e1c995/geolookup/conditions/q/' + state + '/' + city + '.json';
$.ajax({ url : URL, 
dataType : "jsonp", 
success : function(parsed_json) { 
var location = parsed_json['current_observation']['display_location']['full']; 
var temp = parsed_json['current_observation']['temperature_string'];
var wicon = parsed_json['current_observation']['icon_url'];
</script>
</body>
</head>
</html>

このようなリンクに変数を追加して、気象条件のアイコンを変更したい

http://icons-ak.wxug.com/i/c/k/ '; +アイコン変数+'.gif

jQuery(document).ready(function($) {
 var state = 'CA';
 var city = 'San_Francisco';
 var URL = 'http://api.wunderground.com/api/102376e7c0e1c995/geolookup/conditions/q/' + state + '/' + city + '.json';
$.ajax({ url : URL, 
dataType : "jsonp", 
success : function(parsed_json) { 
var location = parsed_json['current_observation']['display_location']['full']; 
var temp = parsed_json['current_observation']['temperature_string'];
var wicon = parsed_json['current_observation']['icon_url'];
var humidity = parsed_json['current_observation']['relative_humidity'];
var wind = parsed_json['current_observation']['wind_string'];
var pressure = parsed_json['current_observation']['pressure_mb'];
var visibility = parsed_json['current_observation']['visibility_km'];
var weicon = parsed_json['current_observation']['icon'];
$("#meteo").text(temp);
$( '<img>' ).attr( 'src', wicon ).appendTo( '#wicon' );
$("#location").text(location);
$("#humidity").text(humidity);
$("#wind").text(wind);
$("#pressure").text(pressure);
$("#visibility").text(visibility);
} }); }); 

var wiconは天気アイコンのリンクであり、varweiconはアイコンの名前です

助けてください

4

2 に答える 2

0

just add this to the success function

\\ get the element that we will change
var pic = document.getElementById('wicon');
\\ update the img to new src
pic.src = wicon
于 2013-01-01T08:35:10.530 に答える
0

I guess you sould change your code to this and thats deppend on what the wicon value could be

<script>
    jQuery(document).ready(function($) {
        var state = 'CA';
        var city = 'San_Francisco';
        var URL = 'http://api.wunderground.com/api/102376e7c0e1c995/geolookup/conditions/q/' + state + '/' + city + '.json';
        $.ajax({
            url: URL,
            dataType: "jsonp",
            success: function(parsed_json) {
                var location = parsed_json['current_observation']['display_location']['full'];
                var temp = parsed_json['current_observation']['temperature_string'];
                var wicon = parsed_json['current_observation']['icon_url'];

                if(wicon !=null) {
                    $("#icoImg").attr("src", wicon);
                }
            }
        });
    }

    );
            </script>


<body>

    <img id="icoImg" src="#"/>
</body>
于 2013-01-01T08:35:30.207 に答える