2017-12-11 4 views
0

<a> 요소 click 이벤트에서 javascript 함수를 호출해야합니다. 페이지가 처음로드되면 °C에 온도가 표시됩니다. 나중에 사용자가 온도를 클릭하여 페이지를 새로 고치지 않고 °C to °F에서 그 반대로 변경할 수 있습니다. 도와 주셔서 감사합니다..!!<a> 요소에서 자바 스크립트 함수 호출 onclick 이벤트

코드 :

HTML :

<html> 
<script src="https://use.fontawesome.com/51b73c8fb8.js"></script> 

<body> 
    <div class="mainContainer"> 
    <div class="tempOutCont"> 
     <div class="tempInCont"> 
     <span id="headDetails"></span> 
     <div id="iconTemp"> 
      <img id="icon"></img> 
      <a id="unitLink" href="https://www.google.com"></a> 
     </div> 
     <span id="mainWeather"></span> 
     <span id="detailsHead">Details</span> 
     <div id="listDetails"> 
      <ul> 
      <li>Wind<span id="windText" class="listDetailsItem"></span></li> 
      <li>Pressure<span id="pressureText" class="listDetailsItem"></span></li> 
      <li>Humidity<span id="humidText" class="listDetailsItem"></span></li> 
      <li>Cloudiness<span id="cloudText" class="listDetailsItem"></span></li> 
      </ul> 
      <div> 
      </div> 
     </div> 
     </div> 
</body> 

</html> 

CSS :

html, 
body { 
    height: 100%; 
    width: 100%; 
    margin: 0; 
    padding: 0; 
} 
.mainContainer { 
    background-color: red; 
    height: 100%; 
    width: 100%; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
} 
.tempOutCont { 
    background-color: white; 
    height: 20em; 
    width: 40em; 
    border-radius: 1em; 
    margin-bottom: 10em; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
} 
.tempInCont { 
    background-color: blue; 
    margin: 2em; 
    width: 100%; 
    height: 80%; 
    border-radius: 1em; 
    display: flex; 
    flex-direction: column; 
} 
#headDetails { 
    border: 0.1em white solid; 
    text-align: left; 
    margin-top: 1em; 
} 
#iconTemp { 
    border: 0.1em white solid; 
    display: flex; 
    justify-content: space-around; 
} 
#mainWeather { 
    text-align: center; 
} 
#detailsHead { 
    text-align: center; 
} 
#listDetails { 
    text-align: center; 
} 
li { 
    margin-left: 10em; 
    text-align: left; 
} 
.listDetailsItem { 
    margin-left: 5em; 
    text-align: left; 
} 
a { 
    color: white; 
} 

JS :

/*<iframe src="https://codepen.io/shaan046/full/jaggro/" allow="geolocation"></iframe>*/ 
var appId = "675645ead57721be136750f9cfc0acca"; 
var unit; 
var temperature; 

var changeUnit = function changeUnit(unit, temperature) { 
    console.log(unit); 
    if (unit === "C") { 
    temperature = temperature * 1.8 + 32; 
    unit = "F"; 
    document.getElementById("unitLink").innerHTML = temperature + "°F"; 
    } else if (unit === "F") { 
    temperature = (temperature - 32)/1.8; 
    unit = "C"; 
    document.getElementById("unitLink").innerHTML = temperature + "°C"; 
    } else if (unit === "K") { 
    temperature = temperature - 273.15; 
    unit = "C"; 
    document.getElementById("unitLink").innerHTML = temperature + "°C"; 
    } 
    return false; 
}; 

var getWeatherData = function getWeatherData() { 
    if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function showPosition(position) { 
     var lat = String(position.coords.latitude); 
     var lon = String(position.coords.longitude); 
     $.ajax({ 
     type: "GET", 
     url: 
      "https://api.openweathermap.org/data/2.5/weather?lat=" + 
      lat + 
      "&lon=" + 
      lon + 
      "&appid=" + 
      appId, 
     success: function(json) { 
      document.getElementById("headDetails").innerHTML = 
      "Weather in " + json.name + "," + json.sys.country; 
      document.getElementById("icon").src = 
      "https://openweathermap.org/img/w/" + json.weather[0].icon + ".png"; 
      changeUnit("K", json.main.temp); 
      document.getElementById("mainWeather").innerHTML = 
      json.weather[0].main; 
      document.getElementById("windText").innerHTML = 
      json.wind.speed + "m/s"; 
      document.getElementById("pressureText").innerHTML = 
      json.main.pressure + "hpa"; 
      document.getElementById("humidText").innerHTML = 
      json.main.humidity + "%"; 
      document.getElementById("cloudText").innerHTML = 
      json.clouds.all + "%"; 
     } 
     }); 
    }); 
    } else { 
    x.innerHTML = "Geolocation is not supported by this browser."; 
    } 
}; 
$(document).ready(function() { 
    getWeatherData(); 
    $("#unitLink").onclick = changeUnit(unit, temperature); 
}); 
+3

무엇 exac없이 한 번에 HTML 요소의 많은에 동작을

  • 추가 할 수 있습니다 tly는 당신이 가지고있는 문제입니까? – Herohtar

  • +0

    함수 호출을 사용하여 설정 한 앵커 텍스트 인 17 ° C 텍스트를 클릭해도 아무런 변화가 없습니다. 함수 호출은 온도를 Kelvin (K)에서 Celcius (C)로 변환하여 텍스트를 올바르게 설정합니다. 그러나 그 후에는 클릭이 발생하지 않습니다. –

    답변

    1

    jQuery를

    사용해보십시오

    $ ('# elementToChangeID'). 텍스트 ("something"); JS와

    <a href="#" id="someLink">link</a> 
    

    : DOM에 이벤트를 바인딩 할

    +0

    답해 주셔서 감사합니다. 내 문제는 여기에 함수가 전달 된 인수가 있다는 것입니다. 어떻게 JS에서 HTML로 값을 전달할 수 있습니까? 나는 가능하지 않을 것이다. –

    +1

    HTML에 obtrusively 바인딩하지 마십시오. 이것은 나쁜 습관입니다. – DanteTheSmith

    +0

    JS에서 설정 한 값으로 HTML 요소를 편집하려고합니까? –

    1

    PROPPER의 방법은

    $('#someLink').click(function(){ 
    // do something 
    }); 
    

    는 HTML에서 onclick를 사용하지 않습니다.

    장점은 프리젠 테이션에서 (HTML) 언어의

  • 믹싱을 분리

    • 행동 (자바 스크립트)이다
    • 대부분의 크로스 브라우저를 처리 할 수있는 jQuery를 같은 자바 스크립트 프레임 워크를 사용하고
    • 당신을 위해 문제를 당신은 코드 중복
  • +0

    시도해 보았습니다 - $ ("# unitLink") 클릭 (changeUnit (단위, 온도)); 나는 그것을 올바르게하고 있나? 클릭시에는 아무런 변화가 없습니다. –

    관련 문제