2016-09-15 61 views
0

我有一个解析的xml文件,显示两次相同的元素。现在我想要一个用onclick声明隐藏其中一个按钮的按钮。有谁知道如何做到这一点?删除已解析的元素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> 

回答

0

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

+0

不幸的是,它不起作用。 –

+0

@JandeVries我的代码是你的工作版本。你可能正在操纵你的xml响应完全错误,我不知道,因为你没有包含它。 –

+0

它显示屏幕上的2个值,所以解析是好的,但按钮不起作用。 –