如题:在Default.aspx页中动态加载Default2.aspx页面时,Default2.aspx页面中的JS代码未被执行!
Default.aspx中的代码如下:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
<script type="text/javascript">
var xmlHttp=null;
function GetXmlHttpObject()
{
try
{
//Firefox, Opera8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
//IE
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
function stateChanged()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById("mainContent").innerHTML=xmlHttp.responseText;
}
}
window.onload=function ()
{
xmlHttp=GetXmlHttpObject();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET","Default2.aspx",true);
xmlHttp.send(null);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="mainContent">
</div>
World!
</form>
</body>
</html>
Default2.aspx中的代码如下:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
<script type="text/javascript">
function lod(){
var oHello= document.getElementById("hello");
oHello.style.color="green";
}
</script>
</head>
<body onload="lod()">
<div id="hello">
Hello
</div>
</body>
</html>
最终执行Default.aspx页面的结果为:
World!
其中Hello的颜色并非绿色啊。。。(Default2.aspx中的JS代码未被执行)