我是新手,所以如果我遗漏了一些明显的东西,请提前道歉,但我无法弄清楚如何在 WebStorm 中运行 JavaScript。 WebStorm documentation说只需在浏览器中打开 HTML 文件,但这似乎不起作用。无论如何,一切都在进行中 codepen.io .
这是 HTML(针对简单的天气应用程序):
<body>
<head>
<script src="script.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<div class="container-fluid">
<div class="col-sm-3">
</div>
<div class="col-sm-6">
<div class="white-box text-center">
<span>Weather where you are:</span>
<div class="loc"></div>
<div class="weather"></div>
<div class="temp"></div>
<br>
</div>
</div>
<div class="col-sm-3">
</div>
</div>
</body>
这是脚本(仍处于草稿阶段,因为它需要扩展以链接到涵盖“天气”所有值的图像):
$(document).ready(function() {
$( window ).on("load", function(){
$.getJSON("http://ip-api.com/json", function(json) {
var json;
json = JSON.stringify(json);
var obj = JSON.parse(json);
var latitude = obj.lat;
var longitude = obj.lon;
$.getJSON("http://api.openweathermap.org/data/2.5/weather?lat="+latitude+"&lon="+longitude+"&appid=74a6725c2ca6f1342464bb9005bf0b63", function(json) {
var json;
json = JSON.stringify(json);
var obj = JSON.parse(json);
var loc = obj.name;
var weather = obj.weather[0].description;
var tempInCelsius = obj.main.temp - 273.15;
var tempInCelsiusString = tempInCelsius.toFixed(1) + " ℃";
var tempInFahrenheit = obj.main.temp * 9/5 - 459.67;
var tempInFahrenheitString = tempInFahrenheit.toFixed(1) + " ℉";
var tempStringCombined = tempInCelsiusString + " | " + tempInFahrenheitString;
$(".loc").html(loc);
if(weather === "haze"){
weather = "<img src='https://cdn3.iconfinder.com/data/icons/chubby-weather/440/fog-512.png'>";
}
$(".weather").html(weather);
$(".temp").html(tempStringCombined);
});
});
});
});
非常感谢您的帮助!
最佳答案
选择 html 文件的选项卡(例如 index.html
),然后单击菜单 Run > Run...
并选择 index.html
.
关于javascript - 如何在 WebStorm 中运行 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42113807/