我使用onclick()从按钮调用javascript。 javascript将通过xml文件并在username下列出子节点中的文本。运行代码时,页面回发到顶部但没有任何反应。
的xml:
<?xml version="1.0" encoding="utf-8"?>
<users>
<user>
<username>chuckbeyor101</username>
<playlist>
<song>
<ink>http://www.mp3jive.com/dl.php?u=aHR0cDovL3d3OC5tcDNqdWljZXMuY29tL2Rvd25sb2FkLzYtOXY0L3AxNi84MjU2MzcwNy8wM2Y5ODIwM2Y2N2NiNC9mb3N0ZXItdGhlLXBlb3BsZS0tLXB1bXBlZC11cC1raWNrcyMubXAz&name=Foster+The+People+-+Pumped+Up+Kicks+mp3</link>
</song>
</playlist>
</user>
</users>
html:
<a href="#" onclick="return myFunction();" ><img src="/images/playlist.png" alt="Add to Playlist" ></a>
<script>
function myFunction() {
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","users.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
x=xmlDoc.getElementsByTagName("users");
i=0;
function displayCD()
{
username=(x[i].getElementsByTagName("username")[0].childNodes[0].nodeValue);
alert(username);
}
}
</script>
答案 0 :(得分:0)
使事件处理函数返回false
以防止默认onclick
处理程序:
function myFunction() {
...
return false;
}
或者,更改您的onclick代码:
onclick="myFunction(); return false;"