I am doing a simple weather application. As you can see from the code below, I am showing 5-day forecast, I want to show as a list, but there is a problem that I can not append an image to my list elements for every day.
my js file
for (var i = 0; i < 5; i++) {
var li = document.createElement("li");
var iconurl = "/" + forecast[i].image.toString() + ".png";
var t = document.createTextNode("Date: " + forecast[i].date.toString().substring(8, 10)
+ "/" + forecast[i].date.toString().substring(5, 7) + " "
+ "Current Temperature: " + forecast[i].temperature.toString() + " "
+ "Maximum: " + forecast[i].temperature_max.toString() + " "
+ "Minimum: " + forecast[i].temperature_min.toString() + " "
+ "Description: " + forecast[i].text.toString() + " ");
li.appendChild(t);
document.getElementById("myUL").appendChild(li);
document.getElementById("myInput").value = "";
my Html file
<div id="myDIV" class="header">
<h2 style="margin:5px">Weather Application</h2>
<p id="title">You can search here.</p>
<input type="text" id="myInput" placeholder="Search...">
<span onclick="gettingJSON()" class="addBtn">Add</span>
</div>
<ul id="myUL">
</ul>
My List output like this. My Question about; For example, I want to add images next to the "light rain" and other 5 days as you can see below picture. How can I add it?
I am doing a simple weather application. As you can see from the code below, I am showing 5-day forecast, I want to show as a list, but there is a problem that I can not append an image to my list elements for every day.
my js file
for (var i = 0; i < 5; i++) {
var li = document.createElement("li");
var iconurl = "https://openweathermap/img/w/" + forecast[i].image.toString() + ".png";
var t = document.createTextNode("Date: " + forecast[i].date.toString().substring(8, 10)
+ "/" + forecast[i].date.toString().substring(5, 7) + " "
+ "Current Temperature: " + forecast[i].temperature.toString() + " "
+ "Maximum: " + forecast[i].temperature_max.toString() + " "
+ "Minimum: " + forecast[i].temperature_min.toString() + " "
+ "Description: " + forecast[i].text.toString() + " ");
li.appendChild(t);
document.getElementById("myUL").appendChild(li);
document.getElementById("myInput").value = "";
my Html file
<div id="myDIV" class="header">
<h2 style="margin:5px">Weather Application</h2>
<p id="title">You can search here.</p>
<input type="text" id="myInput" placeholder="Search...">
<span onclick="gettingJSON()" class="addBtn">Add</span>
</div>
<ul id="myUL">
</ul>
My List output like this. My Question about; For example, I want to add images next to the "light rain" and other 5 days as you can see below picture. How can I add it?
Share Improve this question asked Apr 23, 2018 at 23:04 M.J.WatsonM.J.Watson 4807 silver badges18 bronze badges 2- forecast[i].image.toString(), is only returning like "a10n". And that URL is working when I displayed it on a different list or div. – M.J.Watson Commented Apr 23, 2018 at 23:12
-
In that case, all you need is to create an image (
document.createElement("img");
) and place the resulting string in thesrc
attribute. And append it to its respectiveli
, of course. – tao Commented Apr 23, 2018 at 23:14
3 Answers
Reset to default 5Here is what you're missing:
var image = document.createElement("img");
image.setAttribute("src", iconurl);
li.appendChild(image);
If you want the image before the text, you should append it first.
You need to create an image element, set its src
to the url, and append it to your li
.
for (var i = 0; i < 5; i++) {
var li = document.createElement("li");
var iconurl = "https://openweathermap/img/w/" + forecast[i].image.toString() + ".png";
var img = li.appendChild(document.createElement('img'));
img.src = iconurl;
var t = // ...
You simply didn't used this variable at all iconurl
You can create Image element as
for (var i = 0; i < 5; i++) {
var li = document.createElement("li");
var iconurl = "https://openweathermap/img/w/" + forecast[i].image.toString() + ".png";
var t = document.createTextNode("Date: " + forecast[i].date.toString().substring(8, 10) + "/" + forecast[i].date.toString().substring(5, 7) + " " + "Current Temperature: " + forecast[i].temperature.toString() + " " + "Maximum: " + forecast[i].temperature_max.toString() + " " + "Minimum: " + forecast[i].temperature_min.toString() + " " + "Description: " + forecast[i].text.toString() + " ");
var img = document.createElement("img");
img.src=iconurl;
img.width=20;
img.height=20;
li.appendChild(t);
li.appendChild(img); document.getElementById("myUL").appendChild(li); document.getElementById("myInput").value = "";
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744218162a4563652.html
评论列表(0条)