一个简单的javascript问题--将一段javascript语句类化 可以直接写在<body onkeypress="" 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不明白,能否说的明白一些。上面的javascript函数在其他页面也要用,我不想再每个页面都写上这段代码,只是想把这段代码拿出来做成像类那样,以后各个页面就可以直接调用! 把function KeyPress(here,condition){ if(condition=="txtCodeInput1") { alert("aa");} if (event.keyCode==13) { if(here.value=="") { alert("bb");} else { var name=andy; AddSpace(name,5);} return false; }}放在common.js中,然后在你的web页面中导入common.js,需要调用时调用KeyPress(here,condition)就可以了 上面的javascript函数做成commom.js文件,每个页面引用这个commom.js,就可以各个页面就可直接调用函数commom.js:function KeyPress(here,condition){ if(condition=="txtCodeInput1") { alert("aa");} if (event.keyCode==13) { if(here.value=="") { alert("bb");} else { var name=andy; AddSpace(name,5);} return false; }}function AddSpace(string,length) { var k; for (k=0;k<length;k++) { string=string + " ";} return(string);}1.html<script language="javascript" src="../commom.js"></script><input id="txtCodeInput1" onkeydown="return KeyPress(this,'txtCodeInput1')" type="text" name="txtCodeInput1" runat="server">2.html<script language="javascript" src="../commom.js"></script><input id="txtCodeInput1" onkeydown="return KeyPress(this,'txtCodeInput1')" type="text" name="txtCodeInput1" runat="server"> 不用类化,你这情况引用比较好obj.js=======================function KeyPress(here,condition){ if(condition=="txtCodeInput1") { alert("aa");} if (event.keyCode==13) { if(here.value=="") { alert("bb");} else { var name=andy; AddSpace(name,5);} return false; }}function AddSpace(string,length) { var k; for (k=0;k<length;k++) { string=string + " ";} return(string);}function crtCodeInput(){document.write("<input id=\"txtCodeInput1\" onkeydown=\"return KeyPress(this,'txtCodeInput1')\" type=\"text\" name=\"txtCodeInput1\" runat=\"server\">");}test.htm=======================<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>无标题文档</title><script src="obj.js"></script></head><body><script>crtCodeInput()</script></body></html> 还是不成功,可能我没有理解好,我在调试调试。我的操作过程是:添加一个.js文件,将javascript函数copy进去之后再html页面<script language="javascript" src="obj.js"></script>引用之后控件操作时调用如:<input id="txtCodeInput1" onkeydown="return KeyPress(this,'txtCodeInput1')" type="text" name="txtCodeInput1" runat="server">调试时:初期页面显示没有错误,但我一往控件中输入页面就显示为错误实在不好意思,一个简单的问题耽误大家的时间:) 没有错误代码。只是页面左下角出现出错符号,同时没有调用函数进行处理。也有可能是别的函数的问题,但是如果把所有javascript语句都写在页面中就没有问题。只是单独的拿出来写在.js中就出现了问题。郁闷死了。过几天就要上交程序了,可是这个简单的问题却没有解决 在common.js里先写一个函数function test(a){ document.formname.textname.value=a;}在页面的一个button里的onclick调用这个函数,如果这样没问题,把这个函数换成上面的 KeyPress(here,condition),有问题就说名是函数本生的问题,调试时要慢慢的一点一点的试 mingxuan3000(铭轩) is right. 把函数写在一个函数文件里,比如 fuction.js然后在页面里应用这个页面 <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript" SRC="fuction.js"></SCRIPT>然后就可以直接用函数了 怎么使用JavaScript或者Jquery,给父窗口的一个input或者span绑定一个onclick事件? 请问这个JS用什么方法压缩而来的? js拷贝目录下所有文件 求一个支持JS控制的MP3播放器. 多个不同类型的视频UBB转换如何区分 请高手实现这种!!!网页图片浏览器!!! ■■■用settimeout 定时执行了一个函数,如何使它停止 关于scrollTop 关于Option类的用法 主页面的高度如何根据其中一个frame的高度自动增长或缩短??? 如何让XMLHTTP在FF上也能正常运行? 如何用脚本函数控制下拉框的显示,及动态过滤内容
{ if(condition=="txtCodeInput1")
{ alert("aa");}
if (event.keyCode==13)
{ if(here.value=="")
{ alert("bb");}
else
{ var name=andy;
AddSpace(name,5);}
return false;
}
}放在common.js中,然后在你的web页面中导入common.js,需要调用时调用KeyPress(here,condition)就可以了
commom.js:
function KeyPress(here,condition)
{ if(condition=="txtCodeInput1")
{ alert("aa");}
if (event.keyCode==13)
{ if(here.value=="")
{ alert("bb");}
else
{ var name=andy;
AddSpace(name,5);}
return false;
}
}
function AddSpace(string,length)
{ var k;
for (k=0;k<length;k++)
{ string=string + " ";}
return(string);
}
1.html
<script language="javascript" src="../commom.js"></script>
<input id="txtCodeInput1" onkeydown="return KeyPress(this,'txtCodeInput1')" type="text" name="txtCodeInput1" runat="server">
2.html
<script language="javascript" src="../commom.js"></script>
<input id="txtCodeInput1" onkeydown="return KeyPress(this,'txtCodeInput1')" type="text" name="txtCodeInput1" runat="server">
=======================
function KeyPress(here,condition)
{ if(condition=="txtCodeInput1")
{ alert("aa");}
if (event.keyCode==13)
{ if(here.value=="")
{ alert("bb");}
else
{ var name=andy;
AddSpace(name,5);}
return false;
}
}
function AddSpace(string,length)
{ var k;
for (k=0;k<length;k++)
{ string=string + " ";}
return(string);
}
function crtCodeInput(){
document.write("<input id=\"txtCodeInput1\" onkeydown=\"return KeyPress(this,'txtCodeInput1')\" type=\"text\" name=\"txtCodeInput1\" runat=\"server\">");
}
test.htm
=======================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<script src="obj.js"></script>
</head><body>
<script>
crtCodeInput()
</script>
</body>
</html>
我的操作过程是:添加一个.js文件,将javascript函数copy进去
之后再html页面<script language="javascript" src="obj.js"></script>引用
之后控件操作时调用如:<input id="txtCodeInput1" onkeydown="return KeyPress(this,'txtCodeInput1')" type="text" name="txtCodeInput1" runat="server">
调试时:初期页面显示没有错误,但我一往控件中输入页面就显示为错误实在不好意思,一个简单的问题耽误大家的时间:)
郁闷死了。过几天就要上交程序了,可是这个简单的问题却没有解决
function test(a){
document.formname.textname.value=a;
}
在页面的一个button里的onclick调用这个函数,如果这样没问题,把这个函数换成上面的 KeyPress(here,condition),有问题就说名是函数本生的问题,
调试时要慢慢的一点一点的试
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript" SRC="fuction.js"></SCRIPT>然后就可以直接用函数了