0
我使用xmlhttprequest獲取'cars'數組併爲數組中的每個值動態創建錨定標記。訪問動態創建的錨定標記
現在我無法訪問新創建的錨標籤的innerhtml。
//script for getting array and creating anchor tags dynamically
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var cars = JSON.parse(xmlhttp.responseText);
for (i = 0; i < cars.length; i++)
{
var mydiv = document.getElementById("myDiv");
var aTag = document.createElement('a');
aTag.setAttribute('href','#');
aTag.setAttribute('class','cl');
aTag.innerHTML = cars[i] + "<br>";
mydiv.appendChild(aTag);
}
}
};
xmlhttp.open("POST", "http://localhost:3000/welcomepage", true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.send();
//script for accessing innerhtml of the above created anchor tags.
$('.cl').click(function(e) {
e.preventDefault();
var t = $(this).text();
alert(t);
}
我必須包括$(「# myDiv「)。也使用xmlhttp ..非常感謝你... –