簡體   English   中英

刪除解析的元素onclick

[英]remove parsed element onclick

我有一個解析的xml文件,它顯示兩次相同的元素。 現在我想要一個按鈕,該按鈕用onclick語句隱藏其中一個按鈕。 有誰知道如何做到這一點?

<!DOCTYPE html>
<html>
  <body>

    <p id="dasa"></p>

    <script>
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
         myFunction(this);
        }
      };
      xhttp.open("GET", "customers.xml", true);
      xhttp.send();

      function myFunction(xml) {
         var xmlDoc = xml.responseXML;
         var x = xmlDoc.getElementsByTagName("syl");

         document.getElementById("dasa").innerHTML =
         x[0].getAttribute('category') + "<br>";

         document.getElementById("dasa").innerHTML +=
         x[0].getAttribute('category');
      }

      function remove() {
        x[0].removeAttribute('category');
      }
  </script>

  <button onclick="remove()">remove</button>

  </body>
</html>

x在您的刪除函數中未定義。

function remove() {
   x[0].removeAttribute('category');
}

您想要這樣的東西:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
    myFunction(this);
}
};
xhttp.open("GET", "customers.xml", true);
xhttp.send();

var xmlDoc;
var x;
function myFunction(xml) {
xmlDoc = xml.responseXML;
x = xmlDoc.getElementsByTagName("syl");

document.getElementById("dasa").innerHTML =
x[0].getAttribute('category') + "<br>";

document.getElementById("dasa").innerHTML +=
x[0].getAttribute('category');
}

function remove() {
x[0].removeAttribute('category');
}

這將使x成為myfunction設置的全局myfunction

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM