很郁闷,花了很长时间才找出这个bug:javascript 中写方法时不要把方法名 定义为: function start() ,不信你试试,web页面是不能正确使用的! 我想start()这个名字是保留字或者禁用的把,具体原应不知道。
下面是我今下午做的例子代码:
ajax.js
//
JavaScript Document
var xmlhttp;
function createxmlhttprequest()
... {
if(window.ActiveXObject)
...{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
...{
xmlhttp = new XMLHttpRequest();
}
}
function starts()
... {
alert("begin AJAX .....");
createxmlhttprequest();
xmlhttp.onreadystatechange = handlechange;
xmlhttp.open("get","163.xml",true);
xmlhttp.send(null);
}
function handlechange()
... {
if(xmlhttp.readyState==4)
...{
if(xmlhttp.status==200)
...{
readxml();
}
}
}
function readxml()
... {
var xmldoc=xmlhttp.responseXML;
var node=xmldoc.getElementsByTagName("content")[0];
var content=node.childNodes[0].nodeValue;
//alert(content);
//document.getElementById("div1").innerHTML=content;
document.getElementById("div1").innerHTML = content;
}
var xmlhttp;
function createxmlhttprequest()
... {
if(window.ActiveXObject)
...{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
...{
xmlhttp = new XMLHttpRequest();
}
}
function starts()
... {
alert("begin AJAX .....");
createxmlhttprequest();
xmlhttp.onreadystatechange = handlechange;
xmlhttp.open("get","163.xml",true);
xmlhttp.send(null);
}
function handlechange()
... {
if(xmlhttp.readyState==4)
...{
if(xmlhttp.status==200)
...{
readxml();
}
}
}
function readxml()
... {
var xmldoc=xmlhttp.responseXML;
var node=xmldoc.getElementsByTagName("content")[0];
var content=node.childNodes[0].nodeValue;
//alert(content);
//document.getElementById("div1").innerHTML=content;
document.getElementById("div1").innerHTML = content;
}
test.jsp
<%
...
@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage=""
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > ajax </ title >
< script src ="js/ajax.js" ></ script >
</ head >
< body >
< div id ="all" >
< form action ="#" >
< input name ="" type ="button" value ="open xml" onclick ="starts();" />
</ form >
< a href ="#" onclick ="starts();" > goajax </ a >
< hr />
< div id ="div1" >
waiting open xmlfile.............
</ div >
</ div >
</ body >
</ html >
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > ajax </ title >
< script src ="js/ajax.js" ></ script >
</ head >
< body >
< div id ="all" >
< form action ="#" >
< input name ="" type ="button" value ="open xml" onclick ="starts();" />
</ form >
< a href ="#" onclick ="starts();" > goajax </ a >
< hr />
< div id ="div1" >
waiting open xmlfile.............
</ div >
</ div >
</ body >
</ html >
163.xml
<?
xml version="1.0" encoding="GB2312"
?>
< document >
< title > 网易通行证服务条款 </ title >
< author > 163 </ author >
< content > 猫扑网(mop.com)服务使用协议(修订版) </ content >
</ document >
< document >
< title > 网易通行证服务条款 </ title >
< author > 163 </ author >
< content > 猫扑网(mop.com)服务使用协议(修订版) </ content >
</ document >