admin管理员组文章数量:1399499
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 = "";
本文标签: htmlAppend Image and text together to the list in javascriptStack Overflow
版权声明:本文标题:html - Append Image and text together to the list in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744218162a2595736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论