0
大家好!
我有一个问题,我不知道如何解决它!JS事件。只有一个事件处理函数被调用?
我有一个简单的HTML页面的按钮:
<input type = "submit" value = "ok" onclick="f1()"/>
我也有一个脚本,在此页上:
<script>
function f2()
{
var firstBtn = document.createElement("button");
firstBtn.appendChild(document.createTextNode("firstBtn"));
document.body.appendChild(firstBtn);
var xmlhttp = CreateRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseText;
document.body.innerHTML += response;
}
}
firstBtn.onclick = function()
{
alert("inside f2 onclick");
xmlhttp.open("GET","test.php",true);
xmlhttp.send();
}
}
function f3()
{
var secondBtn = document.createElement("button");
secondBtn.appendChild(document.createTextNode("secondBtn"));
document.body.appendChild(secondBtn);
var xmlhttp = CreateRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseText;
document.body.innerHTML += response;
}
}
secondBtn.onclick = function()
{
alert("inside f3 onclick");
xmlhttp.open("GET","test.php",true);
xmlhttp.send();
}
}
function f1()
{
f2();
f3();
}
function CreateRequest()
{
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();// code for IE7+, Firefox, Chrome, Opera, Safari
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");// code for IE6, IE5
return xmlhttp;
}
</script>
所以现在,当我点击一个按钮(firstBtn或secondBtn),另一个不响应点击事件!有人知道为什么吗?
请参阅萤火虫控制台上的错误。 – 2013-05-04 10:15:47
似乎是点击处理程序中xmlhttp变量的范围问题。 – Ihsan 2013-05-04 10:17:44
你想做什么?这两个按钮是否需要启动sam e功能? – Cherniv 2013-05-04 10:28:56