Javascript和XML按钮
嗨,我已经通过html制作了一个按钮,我正在尝试在单击按钮时显示一个XML页面。我需要通过javascript实现这一点,但它似乎对我不起作用 我的HTML:Javascript和XML按钮,javascript,html,xml,dom,Javascript,Html,Xml,Dom,嗨,我已经通过html制作了一个按钮,我正在尝试在单击按钮时显示一个XML页面。我需要通过javascript实现这一点,但它似乎对我不起作用 我的HTML: <input type="button" value="Shop 1" id ="one"> <input type="button" value="Shop 2" id ="two"> <input type="button" value="Put Selected Pictures in Cart" id
<input type="button" value="Shop 1" id ="one">
<input type="button" value="Shop 2" id ="two">
<input type="button" value="Put Selected Pictures in Cart" id ="three">
任何帮助都将不胜感激。谢谢 由于XMLHttprequests的本质是异步的,您应该在回调函数中处理检索到的数据(或者您可以通过指定
env.open(“GET”,url,true)”使其同步)
。新的getData
函数如下所示:
function getData(url, callback) {
var env = new XMLHttpRequest();
env.onload = function() {
callback(env.response);
};
env.open("GET", url);
}
您可以按如下方式使用它:
var one = document.getElementById('one'),
two = document.getElementById('two');
one.onclick = function(){
getData('shop1.xml', function(response) {
/* do whatever you want with the response XML */
});
}
two.onclick = function(){
getData('shop2.xml', function(response) {
/* do whatever you want with the response XML */
});
}
请参阅以获取更多信息。注意,这将简单地将XMLdoc作为字符串获取;如果需要将其作为XML树进行遍历,则需要使用。请发布您的
getData
函数…函数getData(filename){env=new XMLHttpRequest();env.open(“get”,filename);}您没有共享所有代码,或者您需要确保发送XMLHttpRequest。到目前为止,您只是打开请求,而不是实际发送请求。然后您需要一些实际的代码来显示请求的结果,因为这似乎也丢失了。
var one = document.getElementById('one'),
two = document.getElementById('two');
one.onclick = function(){
getData('shop1.xml', function(response) {
/* do whatever you want with the response XML */
});
}
two.onclick = function(){
getData('shop2.xml', function(response) {
/* do whatever you want with the response XML */
});
}