ASP母版页中如何调用body的onload属性
问题描述:
如题,在没有母版页的界面调用body的onload="init()"属性就可以使用,如以下代码↓
<html>
<head>
<title></title>
<script language="javascript">
<!--
var where = new Array(35);
function comefrom(loca, locacity) { this.loca = loca; this.locacity = locacity; }
where[0] = new comefrom("请选择省份名", "请选择城市名");
where[1] = new comefrom("北京", "|东城|西城|崇文|宣武|朝阳|丰台|石景山|海淀|门头沟|房山|通州|顺义|昌平|大兴|平谷|怀柔|密云|延庆");
.....
function select() {
with (document.creator.province) { var loca2 = options[selectedIndex].value; }
for (i = 0; i < where.length; i++) {
if (where[i].loca == loca2) {
loca3 = (where[i].locacity).split("|");
for (j = 0; j < loca3.length; j++) { with (document.creator.city) { length = loca3.length; options[j].text = loca3[j]; options[j].value = loca3[j]; var loca4 = options[selectedIndex].value; } }
break;
}
}
document.creator.newlocation.value = loca2 + loca4;
}
function init() {
with (document.creator.province) {
length = where.length;
for (k = 0; k < where.length; k++) { options[k].text = where[k].loca; options[k].value = where[k].loca; }
options[selectedIndex].text = where[0].loca; options[selectedIndex].value = where[0].loca;
}
with (document.creator.city) {
loca3 = (where[0].locacity).split("|");
length = loca3.length;
for (l = 0; l < length; l++) { options[l].text = loca3[l]; options[l].value = loca3[l]; }
options[selectedIndex].text = loca3[0]; options[selectedIndex].value = loca3[0];
}
}
-->
</script>
</head>
<form name="creator">
<body onload="init()">
<form runat="server" >
<table>
<tr>
<td>
<select name="province" onChange = "select()"></select></td>
<td>
<select name="city" onChange = "select()"></select></td>
</tr>
<tr>
<td>
<input type=text name="newlocation" maxlength=12 size=12 style="font-weight: bold">
<asp:Button ID="Button1" runat="server" Text="Button" Width="82px" /></td>
<td>
</td>
</tr>
</table>
</form>
</body>
</form>
</html>
在实现的过程中,我认为这里的核心就是得益于这个,但是在母版页中的属性没有办法启用,如下
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server" >
<script language="javascript" type="text/javascript">
var where = new Array(35);
function comefrom(loca, locacity) { this.loca = loca; this.locacity = locacity; }
where[0] = new comefrom("请选择省份名", "请选择城市名");
where[1] = new comefrom("北京", "|东城|西城|崇文|宣武|朝阳|丰台|石景山|海淀|门头沟|房山|通州|顺义|昌平|大兴|平谷|怀柔|密云|延庆");
...
function select() {
with (document.creator.province) { var loca2 = options[selectedIndex].value; }
for (i = 0; i < where.length; i++) {
if (where[i].loca == loca2) {
loca3 = (where[i].locacity).split("|");
for (j = 0; j < loca3.length; j++) { with (document.creator.city) { length = loca3.length; options[j].text = loca3[j]; options[j].value = loca3[j]; var loca4 = options[selectedIndex].value; } }
break;
}
}
document.creator.newlocation.value = loca2 + loca4;
}
window.onload = function init() {
with (document.creator.province) {
length = where.length;
for (k = 0; k < where.length; k++) { options[k].text = where[k].loca; options[k].value = where[k].loca; }
options[selectedIndex].text = where[0].loca; options[selectedIndex].value = where[0].loca;
}
with (document.creator.city) {
loca3 = (where[0].locacity).split("|");
length = loca3.length;
for (l = 0; l < length; l++) { options[l].text = loca3[l]; options[l].value = loca3[l]; }
options[selectedIndex].text = loca3[0]; options[selectedIndex].value = loca3[0];
}
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server" onload="init()" >
<form name="creator">
<body onload="init()">
<form>
<table>
<tr>
<td>
<select name="province" onChange = "select()"></select></td>
<td>
<select name="city" onChange = "select()"></select></td>
</tr>
<tr>
<td>
<input type=text name="newlocation" maxlength=12 size=12 style="font-weight: bold">
<asp:Button ID="Button1" runat="server" Text="Button" /></td>
<td>
</td>
</tr>
</table>
</form>
</body>
</form>
</asp:Content>
有了母版页,我认为是body的onload属性没有了,导致无法加载,老哥们这种情况应该如何解决
在网上搜 用了 window.onload = function init() 并不行
答
你好,
我认为你可以直接将onload 写在ContentPlaceHolder里面.
内容页:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script>
document.body.onload = function () {
alert("ok");
}
</script>
<table>
<tr>
<td>
<select name="province"></select></td>
<td>
<select name="city"></select></td>
</tr>
<tr>
<td>
<input type=text name="newlocation" maxlength=12 size=12 style="font-weight: bold">
<asp:Button ID="Button1" runat="server" Text="Button" /></td>
<td>
</td>
</tr>
</table>
</asp:Content>