简体   繁体   中英

javascript onclick button not working

I have a xml file named devoir.xml

My code is

<!DOCTYPE html>
<html>
<style>
table,th,td {
  border : 1px solid black;
  border-collapse: collapse;
}
th,td {
  padding: 5px;
}
</style>
<body>
<button type="button" value="button text" onclick="loadXMLDoc()";>Avoir les nouvelles.</button>
<table id="demo"></table>
<script language="javascript">
function loadXMLDoc() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 and xmlhttp.status == 200) {
      myFunction(xmlhttp);
    }
  };
  xmlhttp.open("GET", "devoir.xml", true);
  xmlhttp.send();
}
function myFunction(xml) {
  var xmlDoc = xml.responseXML;
  var table="<tr><th>Titre</th><th>Description</th></tr>";
  var x = xmlDoc.getElementsByTagName("item");
  for ( var i = 0 ; i &lt; x.length ; i++) { 
    table += "<tr><td>" +
    x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue +
    "</td></tr>";
  }
  document.getElementById("demo").innerHTML = table;
}
</script>
</body>
</html>

The page load but when i push on the button there is nothing happening at all. Any idea what's going on?

I have try to change somestuff into my code but always the same result. I have try another thing online and it's not working also. Any idea?

Try to replace &lt; by < in :

for ( var i = 0 ; i &lt; x.length ; i++) { 

Should be :

for ( var i = 0 ; i < x.length ; i++) { 

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM