当前位置: 代码迷 >> Java Web开发 >> input 的onclick事件 为何不能用
  详细解决方案

input 的onclick事件 为何不能用

热度:126   发布时间:2016-04-16 22:20:26.0
input 的onclick事件 为什么不能用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.test{
background-color: blue;
}
</style>

</head>
<body>
<input type="button" onclick="showMsg()" value="调用Ajax显示内容" />
<span id="msg"></span>

<script type="text/javascript">
var xmlHttp;
function createXMLHttp(){
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}else{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function showMsg(){
createXMLHttp();
xmlHttp.open("POST","content.html");
xmlHttp.onreadystatechange= showMsgCallback;
xmlHttp.send(null);
}
function showMsgCallback(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status ==200){
var text =xmlHttp.responseText;
document.getElementById("msg").className="test";
document.getElementById("msg").innerHTML=text;
}
}
}
</script>
</body>
</html>

------解决方案--------------------
肯定执行了,你可以alert或者console.log debug
------解决方案--------------------
估计你的showMsg 方法里面有问题,在showMsg() 方法内打alert() ,一步步调试,看到哪一步出错的,然后解决问题。
------解决方案--------------------
你的当前页面和content.html都在什么目录下呢?将他们放到test工程的根目录下面,也就是WebContent或WebRoot目录下面,再试试,看能否跳转过去?可以的话,可能就是路径问题了;如果不可以,那就看content.html文件是否有问题了。
------解决方案--------------------
引用:
Quote: 引用:

要么后台报错, 要么js报错, chrome调试走起


请求为什么提交到html呢?  content.html 是做什么处理的?
  相关解决方案