2013-05-19 2 views
-4

을 나는 Simpleweather.js 플러그인을 사용하고 있는데, 그것은이 같은 JS를 사용합니다 :반복 코드를 중지하는 함수를 작성하고 있습니까? - JS

$.simpleWeather({ 
zipcode: '', 
woeid: '12289', 
location: '', 
unit: 'c', 
success: function(weather) { 
    html = '<h2 class="none">'+weather.city+'</h2>'; 
    html += '<img src="assets/img/'+weather.code+'.png">'; 
    html += '<p>'+weather.temp+'&deg; '+weather.units.temp+'<br /><span>'+weather.currently+'</span></p>'; 
    html += '<p>Wind: '+weather.wind.speed+weather.units.speed+'</p>'; 
    html += '<div class="tmr"><p>Tomorrow:</p><img src="assets/img/'+weather.tomorrow.code+'.png">'+'<p>'+weather.tomorrow.high+'&deg;'+weather.units.temp+'<br />'+weather.tomorrow.forecast+'</p></div>'; 
    html += '<a href="'+weather.link+'">Full Forecast</a>'; 

    $("#weather").html(html); 
}, 
error: function(error) { 
    $("#weather").html('<p>'+error+'</p>'); 
} 
}); 

그리고 간단한 HTML을

<div id="weather"></div> 

그것을 표시 등.

그러나 여러 장소의 날씨를 표시하고 싶습니다. 여기서는 5 자리 숫자 인 woeid와 여기에 #weather로 표시된 div ID를 표시합니다.

weatherFunction(divid,woeid); 

그것은 입력으로 두 가지를 받아 바로 위의 코드에 추가합니다 : 내가 뭘하려는 이와 같은 호출시에있는 함수를 작성합니다. 나는 JS에서 아주 간단한 함수를 작성했고, 이것에 대한 도움을 정말로 원할 것이다. 나는 이론적으로 그것을하는 법을 알고 있지만 실제로는 그렇지 않습니다 - 누군가 나를 도와 주거나 나에게 출발점을 줄 수 있습니까? 미리 환호!

+0

당신은 시도조차하지 않았습니다. 그렇다고해서 질문을하는 것이 아닙니다. – Zim84

+1

죄송 합니다만, 함수 내에서 코드를 복사하여 붙여 넣기하는 것부터 시작해야 할 곳이 어디인지 알지 못했습니다. – user2383338

답변

1
var weatherFunction = function (divid, woeid) { 

    $.simpleWeather({ 
     zipcode: '', 
     woeid: woeid, 
     location: '', 
     unit: 'c', 
     success: function(weather) { 
      html = '<h2 class="none">'+weather.city+'</h2>'; 
      html += '<img src="assets/img/'+weather.code+'.png">'; 
      html += '<p>'+weather.temp+'&deg; '+weather.units.temp+'<br /><span>'+weather.currently+'</span></p>'; 
      html += '<p>Wind: '+weather.wind.speed+weather.units.speed+'</p>'; 
      html += '<div class="tmr"><p>Tomorrow:</p><img src="assets/img/'+weather.tomorrow.code+'.png">'+'<p>'+weather.tomorrow.high+'&deg;'+weather.units.temp+'<br />'+weather.tomorrow.forecast+'</p></div>'; 
      html += '<a href="'+weather.link+'">Full Forecast</a>'; 

      $(divid).html(html); 
     }, 
     error: function(error) { 
      $(divid).html('<p>'+error+'</p>'); 
     } 
    }); 

} 
+1

고맙습니다. 그게 다야! 함수 내에서 동일한 코드를 사용하는 경우에 불과하다는 것을 알지 못했습니다. 정말 복잡했습니다. – user2383338

+0

네, 정말 쉽습니다! –

관련 문제