javascript - 在 <a> 元素 onclick 事件上调用 Javascript 函数

标签 javascript html jquery anchor jquery-events

我需要在 <a> 上调用 Javascript 函数元素click事件。当页面最初加载时,它将在 °C 中显示温度。 。稍后用户可以单击温度将其更改为 °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");

关于javascript - 在 <a> 元素 onclick 事件上调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47756059/

相关文章:

html - 修复了页脚未被滚动内容下推的问题

jquery - 在调整大小时保持图像覆盖在 div 中

javascript - 查找仅具有部分已知值的字符串

java - 我如何使用 Apache Rhino 调用子属性函数

javascript - 卸载事件之前的 iframe

javascript - 生成表单永久链接

jQuery 列表图标更改

javascript - 如何使用 Jquery 隐藏 div block 中的内联文本

javascript - 添加新行后拖放不起作用

javascript - 如何在 Javascript 中用单个变量编写多个参数?