我正在使用ajax请求从服务器加载数据。 php文件提供了一个xml文件。我成功地获得了所有数据,但我无法在屏幕上获得推特。
在for ...循环的下面的代码中,我做了一些评论,我尝试做什么。 有人可以解释我如何使脚本加载图像,我使用javascript添加dinamicly。
使用appendChild在按钮底部添加valIMG。
function updatePlaneten(){
var valDiv, planets , valButton, textNode;
// Get xml files
planets = this.responseXML.getElementsByTagName("planeet");
// loop through the <planet> tags
for(var i=0;i<planets.length;i++){
(function(num){
valDiv = document.createElement("div");
valDiv.setAttribute("class", "divPresentPlanet" );
// I try to add an img element here.
// the variable imagename containts the path to the img: 'images/moon.jpg'
// appending it does add it to the div, tho the image isnt loaded
var valIMG = document.createElement("image");
var imagename = planets[num].getElementsByTagName("img")[0].childNodes[0].nodeValue;
valIMG.setAttribute("src", imagename );
valIMG.setAttribute("class", "IMGTAGPresentPlanet" );
// Get the id and the name from the xml info in current <planet> tag
var IDplanet = (planets[num].getElementsByTagName("id")[0].childNodes[0].nodeValue)-1;
var id = planets[num].getElementsByTagName("id")[0].childNodes[0].nodeValue + "<br>";
var name = planets[num].getElementsByTagName("name")[0].childNodes[0].nodeValue + "<br>";
var img = planets[num].getElementsByTagName("img")[0].childNodes[0].nodeValue + "<br>";
valDiv.innerHTML = id+"<br>"+name+"<br>"+ img +"<br>";
// Create button with a value and pass in this object for later reference use (valButton.object=this)
valButton = document.createElement("input");
// valButton.setAttribute("planeetID", planets[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
valButton.setAttribute("value", 'Meer info');
valButton.setAttribute("type", 'button');
valButton.id = num;
valButton.object = this;
// FIX: PASS showinfo TO AN ANONYMOUS FUNCTION CONTAINING THE OBJECT
valButton.addEventListener('click', function(){
showinfo(valButton, planets, IDplanet);
});
// Place the button on screen
valDiv.appendChild(valButton);
valDiv.appendChild(valIMG);
// place img tag on screen
valDiv.appendChild(valIMG);
document.getElementById("planetenID").appendChild(valDiv);
}(i));
}
}
答案 0 :(得分:0)
您的图片路径应该具有绝对或相对于您的域名,例如'/images/moon.jpg'
在您的情况下,您有一些网址yourdomain.com/something
,如果您放置{{1},则您的网址不正确}}
您应该<{3}}
document.createElement(&#39; img&#39;)
src="image/moon.jpg"