常用JS代码集(二)
时间:2006-01-16 来源:xrsf
只能是写限定的东西 代码如下:
ENTER键可以让光标移到下一个输入框
只能是中文
只能是英文和数字.屏蔽了输入法
只能输入英文和数字
只能是数字
只能显示,不能修改的文本框
只能是数字,判断按键 代码如下:
function onlyNum()
{
if(!((event.keyCode>=48&&event.keyCode=96&&event.keyCode
附:获取键盘的KeyCode
ns4 = (document.layers) ? true : false;
ie4 = (document.all) ? true : false;
function keyDown(e){
if(ns4){
var nkey=e.which;
var iekey='现在是ns浏览器';
var realkey=String.fromCharCode(e.which);
}
if(ie4){
var iekey=event.keyCode;
var nkey='现在是ie浏览器';
var realkey=String.fromCharCode(event.keyCode);
if(event.keyCode==32){realkey='\' 空格\''}
if(event.keyCode==13){realkey='\' 回车\''}
if(event.keyCode==27){realkey='\' Esc\''}
if(event.keyCode==16){realkey='\' Shift\''}
if(event.keyCode==17){realkey='\' Ctrl\''}
if(event.keyCode==18){realkey='\' Alt\''}
}
alert('ns浏览器中键值:'+nkey+'\n'+'ie浏览器中键值:'+iekey+'\n'+'实际键为'+realkey);
}
document.onkeydown = keyDown;
if(ns4){
document.captureEvents(Event.KEYDOWN);}
//Javascript Document.
请按任意一个键。。。。
限制网页用键盘
限制键盘的某个键:
再加个找按键的值
function show(){
alert("ASCII代码是:"+event.keyCode);
}
只能是IP地址
.a3{width:30;border:0;text-align:center}
function mask(obj){
obj.value=obj.value.replace(/[^\d]/g,'')
key1=event.keyCode
if (key1==37 || key1==39)
{ obj.blur();
nextip=parseInt(obj.name.substr(2,1))
nextip=key1==37?nextip-1:nextip+1;
nextip=nextip>=5?1:nextip
nextip=nextip=3)
if(parseInt(obj.value)>=256 || parseInt(obj.value)=5?1:nextip
nextip=nextip
IP地址输入
IP地址输入
.
.
.
用#default#savehistory防止后退清空text文本框:
.saveHistory {behavior:url(#default#savehistory);}
TEXTAREA自适应文字行数的多少
上传预览图片
去掉下拉选项的边框
cnpeople
cnrose
cnbruce
下拉列表打开窗口
更多链接……
cnbrucecnbruce
daonetdaonet
blue!dea/]http://www.blueidea.com">blue!dea[/url]
TAB的显示
function editTab()
{
var code, sel, tmp, r
var tabs=""
event.returnValue = false
sel =event.srcElement.document.selection.createRange()
r = event.srcElement.createTextRange()
switch (event.keyCode)
{
case (8) :
if (!(sel.getClientRects().length > 1))
{
event.returnValue = true
return
}
code = sel.text
tmp = sel.duplicate()
tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top)
sel.setEndPoint("startToStart", tmp)
sel.text = sel.text.replace(/^\t/gm, "")
code = code.replace(/^\t/gm, "").replace(/\r\n/g, "\r")
r.findText(code)
r.select()
break
case (9) :
if (sel.getClientRects().length > 1)
{
code = sel.text
tmp = sel.duplicate()
tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top)
sel.setEndPoint("startToStart", tmp)
sel.text = "\t"+sel.text.replace(/\r\n/g, "\r\t")
code = code.replace(/\r\n/g, "\r\t")
r.findText(code)
r.select()
}
else
{
sel.text = "\t"
sel.select()
}
break
case (13) :
tmp = sel.duplicate()
tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top)
tmp.setEndPoint("endToEnd", sel)
for (var i=0; tmp.text.match(/^[\t]+/g) && i
alert("ok")
文本框的上/下拉
向下
=4)document.cnbruce.com.rows-=4;else return false'>向上
一个复选框,点击之后一组复选框全部都选上
a
Debug Textarea:在线写 js 脚本的时候,用来即时查错的东西!
Debug Textarea
* {
font-size: 12px
}
body {
margin: 10px; padding: 0px
}
table.list{
cursor: default;
border:1px solid #cccccc
background-color: #cccccc;
border-collapse: collapse;
border-Color: #cccccc;
}
// Coded by windy_sk 20040205
function reportError(msg,url,line) {
var str = "You have found an error as below: \n\n";
str += "Err: " + msg + " on line: " + line;
alert(str);
return true;
}
window.onerror = reportError;
document.onkeydown = function() {
if(event.ctrlKey){
switch(event.keyCode) {
case 82: //r
runcode();
break;
case 83: //s
savecode();
break;
case 71: //g
goto(prompt('Please input the line number', '1'));
break;
case 65: //a
document.execCommand("SelectAll");
break;
case 67: //c
document.execCommand("Copy");
break;
case 88: //x
document.execCommand("Cut");
break;
case 86: //v
document.execCommand("Paste");
break;
case 90: //z
document.execCommand("Undo");
break;
case 89: //y
document.execCommand("Redo");
break;
default:
break;
}
event.keyCode = 0;
event.returnValue = false;
}
return;
}
function show_ln(){
var txt_ln = document.getElementById('txt_ln');
var txt_main = document.getElementById('txt_main');
txt_ln.scrollTop = txt_main.scrollTop;
while(txt_ln.scrollTop != txt_main.scrollTop) {
txt_ln.value += (i++) + '\n';
txt_ln.scrollTop = txt_main.scrollTop;
}
return;
}
function editTab(){
var code, sel, tmp, r
var tabs=''
event.returnValue = false
sel =event.srcElement.document.selection.createRange()
r = event.srcElement.createTextRange()
switch (event.keyCode){
case (8) :
if (!(sel.getClientRects().length > 1)){
event.returnValue = true
return
}
code = sel.text
tmp = sel.duplicate()
tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top)
// 出错检验代码~~~~~~
if(sel.parentElement() != tmp.parentElement()) return;
sel.setEndPoint('startToStart', tmp)
sel.text = sel.text.replace(/^\t/gm, '')
code = code.replace(/^\t/gm, '').replace(/\r\n/g, '\r')
r.findText(code)
r.select()
break
case (9) :
if (sel.getClientRects().length > 1){
code = sel.text
tmp = sel.duplicate()
tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top)
// 出错检验代码~~~~~~
if(sel.parentElement() != tmp.parentElement()) return;
sel.setEndPoint('startToStart', tmp)
sel.text = '\t'+sel.text.replace(/\r\n/g, '\r\t')
code = code.replace(/\r\n/g, '\r\t')
r.findText(code)
r.select()
}else{
sel.text = '\t'
sel.select()
}
break
case (13) :
tmp = sel.duplicate()
tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top)
// 出错检验代码~~~~~~
if(sel.parentElement() != tmp.parentElement()) return;
tmp.setEndPoint('endToEnd', sel)
for (var i=0; tmp.text.match(/^[\t]+/g) && i");
code_win.document.writeln("function reportError(msg,url,line){\nline-=14;\nvar str='You have found an error as below:
[url=file://n//n';/nstr+='Err]\\n\\n';\nstr+='Err[/url]
: '+msg+' on line: '+(line);\nalert(str);\nopener.goto(line);\nopener.focus();\nwindow.onerror=null;\nsetTimeout('self.close()',10);\nreturn true;\n}");
code_win.document.writeln("window.onerror = reportError;");
code_win.document.writeln("");
code_win.document.writeln(str);
code_win.document.close();
return;
}
function savecode() {
var str = document.getElementById("txt_main").value;
var code_win = window.open('about:blank','_blank','top=10000');
code_win.document.open();
code_win.document.writeln(str);
code_win.document.close();
code_win.document.execCommand('saveas','','code.html');
code_win.close();
return;
}
function goto(ln) {
if(!/^\d+$/.test(ln)) return;
var obj = document.getElementById("txt_main");
var rng = obj.createTextRange();
var arr = obj.value.replace(/\r/, "").split(/\n/);
if(ln>arr.length) ln = arr.length;
var str_tmp = "";
for(var i=0; i\nalert(')\n";}
Debug Textarea
for(var i=1; i
获取input的非value值,而是显示文本值
asdf
bbbb
ccc
select里的option进行分类列表
------------请选择------------
原 创
经 典
asp&sql
php&mysql
动态添加
Please Select...
AA
BB
CC
>">
function addItem(fromName,toName){
eval("var obj1=myForm."+fromName);
eval("var obj2=myForm."+toName);
obj1.options[0].selected=false;
if(obj1.selectedIndex == -1)
return false;
var tempValue;
var tempText;
for(var i=1;i
提示限制输入的字符数
255) n++
}
return n
}
function changebyte(value,length){
var l=strlength(value)
if (l
changebyte1(document.test.icqcontent.value,198);
判断填写字数的限制
function gbcount(message,total,used,remain)
{
var max;
max = total.value;
if(message.value.length > max){
message.value = message.value.substring(0,max);
used.value = max;
remain.value = 0;
alert('留言不能超过规定的字数!');
}
else{
used.value = message.value.length;
remain.value = max - used.value;
}
}
最多字数:
已用字数:
剩余字数:
相关阅读 更多 +