簡體   English   中英

在<a>元素 onclick 事件</a>上調用 Javascript 函數

[英]Call Javascript function on <a> element onclick event

我需要在<a>元素click事件上調用一個 Javascript 函數。 當頁面最初加載時,它將顯示以°C為單位的溫度。 稍后用戶可以單擊溫度將其從°C to °F更改°C to °F ,反之亦然,無需刷新頁面。

代碼 :

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

嘗試使用 jQuery

$('#elementToChangeID').text("something");

將事件綁定到 DOM 的正確方法是:

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

與JS:

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

不使用 HTML 中的onclick

優點是

  • 行為(Javascript)與表示(HTML)分離
  • 沒有語言混合
  • 您正在使用像 jQuery 這樣的 Javascript 框架,它可以為您處理大多數跨瀏覽器問題
  • 您可以一次向大量 HTML 元素添加行為而無需重復代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM