我在学习javascript今天做了个小例子如果把javascript脚本写在asp.net页面没有错误.但是把它分离开来就出错了说缺少对象好郁闷啊.找了好长时间也没找到啊.请求帮忙啊!!!!!速度
代码:asp.net页面
<%@ Page language="c#" Codebehind="div3.aspx.cs" AutoEventWireup="false" Inherits="ADO.div3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>div3</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link href=div1.css type=text/css rel=stylesheet>
<script src="div1.js" language="javascript">
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div id="content">
孤雁儿
<br>
<br>
世人作梅词,下笔便俗。予试作一篇,乃知前言不妄耳。<br>
<br>
藤床纸帐朝眠起,<br>
说不尽、无佳思。<br>
沈香烟断玉炉寒,<br>
伴我情怀如水。<br>
笛声三弄,<br>
梅心惊破,<br>
多少春情意。<br>
<br>
小风疏雨萧萧地,<br>
又催下、千行泪。<br>
吹箫人去玉楼空,<br>
肠断与谁同倚?一枝折得,<br>
人间天上,<br>
没个人堪寄。
</div>
<div id="intro"><hr>
作者:李清照<span id="key" onclick="shoppingcat();">展开</span><br>
来源:中国诗辞网</div>
</form>
</body>
</HTML>js页面:
var s=40;
var minheight=40;
var maxheight=500;
function shoppingcat(){
var key = document.getElementById("key").innerText;
if(key=="展开"){
content.style.pixelHeight+=s;
if(content.style.pixelHeight<maxheight){
setTimeout("shoppingcat();",0);
}else{
document.getElementById("key").innerText="关闭";
}
}else{
content.style.pixelHeight-=s;
if(content.style.pixelHeight>minheight){
setTimeout("shoppingcat();",1);
}else{
document.getElementById("key").innerText="展开";
}
}
}
代码:asp.net页面
<%@ Page language="c#" Codebehind="div3.aspx.cs" AutoEventWireup="false" Inherits="ADO.div3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>div3</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link href=div1.css type=text/css rel=stylesheet>
<script src="div1.js" language="javascript">
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div id="content">
孤雁儿
<br>
<br>
世人作梅词,下笔便俗。予试作一篇,乃知前言不妄耳。<br>
<br>
藤床纸帐朝眠起,<br>
说不尽、无佳思。<br>
沈香烟断玉炉寒,<br>
伴我情怀如水。<br>
笛声三弄,<br>
梅心惊破,<br>
多少春情意。<br>
<br>
小风疏雨萧萧地,<br>
又催下、千行泪。<br>
吹箫人去玉楼空,<br>
肠断与谁同倚?一枝折得,<br>
人间天上,<br>
没个人堪寄。
</div>
<div id="intro"><hr>
作者:李清照<span id="key" onclick="shoppingcat();">展开</span><br>
来源:中国诗辞网</div>
</form>
</body>
</HTML>js页面:
var s=40;
var minheight=40;
var maxheight=500;
function shoppingcat(){
var key = document.getElementById("key").innerText;
if(key=="展开"){
content.style.pixelHeight+=s;
if(content.style.pixelHeight<maxheight){
setTimeout("shoppingcat();",0);
}else{
document.getElementById("key").innerText="关闭";
}
}else{
content.style.pixelHeight-=s;
if(content.style.pixelHeight>minheight){
setTimeout("shoppingcat();",1);
}else{
document.getElementById("key").innerText="展开";
}
}
}
src="/div1.js"
src="../div1.js" 测试下...
var s=40;
var minheight=40;
var maxheight=500;
function shoppingcat(){
var key = document.getElementById("key").innerText;
if(key=="展开"){
content.style.pixelHeight+=s;
if(content.style.pixelHeight <maxheight){
setTimeout("shoppingcat();",0);
}else{
document.getElementById("key").innerText="关闭";
}
}else{
content.style.pixelHeight-=s;
if(content.style.pixelHeight >minheight){
setTimeout("shoppingcat();",1);
}else{
document.getElementById("key").innerText="展开";
}
}
}
------------你的js里面找document.getElementById("key").innerText; 这个控件id找不到吧?
改成这样试试
js页面:
var s=40;
var minheight=40;
var maxheight=500;
function shoppingcat(controlID){
var key = document.getElementById(controlID).innerText;
if(key=="展开"){
content.style.pixelHeight+=s;
if(content.style.pixelHeight <maxheight){
setTimeout("shoppingcat();",0);
}else{
document.getElementById(controlID).innerText="关闭";
}
}else{
content.style.pixelHeight-=s;
if(content.style.pixelHeight >minheight){
setTimeout("shoppingcat();",1);
}else{
document.getElementById(controlID).innerText="展开";
}
}
}
调用时:作者:李清照 <span id="key" onclick="shoppingcat("key");" >展开 </span > <br >
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<style>
#content,#intro{
font: 13px/25px;
width: 200px;
height: 40px;
background: #eee;
padding: 10px;
border: 4px #ccc double;
border-bottom:0;
overflow: hidden;
}#intro {
color: #036;
font:12px;
border: 4px #ccc double;
border-top:0;
height:70px;
}
#key{color:#900;float:right}
hr{height:1px;border:1px #ccc dotted}
</style>
<script src=qq.js></script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div id="content">
孤雁儿
<br>
<br>
世人作梅词,下笔便俗。予试作一篇,乃知前言不妄耳。<br>
<br>
藤床纸帐朝眠起,<br>
说不尽、无佳思。<br>
沈香烟断玉炉寒,<br>
伴我情怀如水。<br>
笛声三弄,<br>
梅心惊破,<br>
多少春情意。<br>
<br>
小风疏雨萧萧地,<br>
又催下、千行泪。<br>
吹箫人去玉楼空,<br>
肠断与谁同倚?一枝折得,<br>
人间天上,<br>
没个人堪寄。
</div>
<div id="intro"><hr>
作者:李清照<span id="key" onclick="shoppingcat();">展开</span><br>
来源:中国诗辞网</div>
</form>
</body>
</HTML>
js代码页面:var s=40;
var minheight=40;
var maxheight=500;
function shoppingcat(){
var key = document.getElementById("key").innerText;
if(key=="展开"){
content.style.pixelHeight+=s;
if(content.style.pixelHeight<maxheight){
setTimeout("shoppingcat();",0);
}else{
document.getElementById("key").innerText="关闭";
}
}else{
content.style.pixelHeight-=s;
if(content.style.pixelHeight>minheight){
setTimeout("shoppingcat();",1);
}else{
document.getElementById("key").innerText="展开";
}
}
}
把"禁止脚本调试(Internet Explore)"和"禁止脚本调试(其他)"的钩去掉.