妙用javascript

http://www.blogjava.net/Jkallen/archive/2006/03/06/33886.aspx?Pending=true#Post
以下是本人在代码中常用到的一些JS脚本,虽然是一些简单而又短小的function却是帮了我不少的忙呢!希望对大家有些用,有了好的再贴上来 ^_^
1:在一带到复杂的FRAME页面中OPEN一个窗体来(当然不用什么parent.parent啦)
None.gif function  checkuser(name)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        window.open(
" about:blank " , " zhutong " , 'menubar = 0 ,width = 350 ,height = 240 ,left = 230 ,top = 200 ,resizable = 0 ,scrollbars = auto');
InBlock.gif        window.document.forms['form1'].target 
=   " zhutong " ;
InBlock.gif        window.document.forms['form1'].action 
=   " Account/check.jsp?username= " + name;
InBlock.gif        window.document.forms['form1'].submit();        
ExpandedBlockEnd.gif    }

以下的LINK来触发
None.gif < href ="javascript:checkuser(window.document.forms['SignUpForm'].name.value);" >
None.gif                                        
< bean:message  key ="account.signup.input.name.check"   />
None.gif                                    
</ a >

当然下面的不能少啦!
None.gif </ html:form >
None.gif
< form  name ="form1"  method ="post"  action ="" >
None.gif
</ form >

2:关闭一当前窗口,并且无关闭提示(这个功能太常用了)
None.gif < input  type ="submit"  name ="Submit"  value ="知道啦"  onClick ='window.opener = "xxx" ;
None.gif    window.close();'
>


3:让一个链接自动提交:
如有一个链接:

None.gif < href ="xx.htm"  name ="multiuse" > AAA </ a >


则可以:

None.gif var  urlfir  =  studyezurl  +  ' / Authentication. do ? actionid = ForgetPassword';
None.gif
var  urlsec  =  studyezurl  +  ' / tempURL. do ';
None.gifmultiuse.href 
=  urlsec;
None.gifmultiuse.focus();
None.gifmultiuse.click();
None.gifmultiuse.href 
=  urlfir;   // 这个好象不起作用,所以最好将此urlfir设成在href中的默认值
None.gif


在STRUTS中有一个多个FRAME构成的系统.点击注销后想固定到某一页面!(这个鬼费了我不少时间 ^_^ )


4:让HTML也有JSP中的request.getParameter("");
HTML也可以通过JS来到得参数,函数如下(我将它保存到了一个数组里面):

None.gif // --------------------------------------------------------------------------
None.gif//
Name: GetArgsFromHref
None.gif//
None.gif//
Pupers: the function will get the parameters from the server
None.gif//
None.gif//
Parameter:
None.gif//
sHref---->>the current URL
None.gif//
sArgName---->>the Array store the name of parameters which you want to get
None.gif//
None.gif//
Return: Array to store the result
None.gif//
None.gif//
Author: Jkallen
None.gif//
----------------------------------------------------------------------------
None.gif
function  GetArgsFromHref(sHref, sArgName)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var args  = sHref.split("?");
InBlock.gif    
var p_Result = new Array();
InBlock.gif    
InBlock.gif    
if(args[0== sHref) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
return p_Result; 
ExpandedSubBlockEnd.gif    }
  
InBlock.gif    
var str = args[1];
InBlock.gif    args 
= str.split("&");
InBlock.gif    
for(var i = 0; i < args.length; i ++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        str 
= args[i];
InBlock.gif        
var arg = str.split("=");
InBlock.gif        
if(arg.length <= 1
InBlock.gif            
continue;
InBlock.gif        
if(sArgName != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for(var c1 = 0; c1 < sArgName.length; c1++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
var tempValue = arg[0];
InBlock.gif                
if( tempValue == sArgName[c1])
InBlock.gif                    p_Result[tempValue] 
= arg[1];
ExpandedSubBlockEnd.gif            }
   
ExpandedSubBlockEnd.gif        }
//end if
ExpandedSubBlockEnd.gif
     }
//end for
InBlock.gif
     
InBlock.gif     
return p_Result;
ExpandedBlockEnd.gif }
// end GetArgsFromHref
None.gif


所以你以后就可以通过如:

None.gif var  curCourseID  =  pValue[c_courseID];


的样子来直接取了啦,c_courseID是参数名称哦

5:在页面动态显示当前时间

ExpandedBlockStart.gif ContractedBlock.gif < span  id ="aa" > 当前时间 </ span >< script > dot.gif setInterval("aa.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);
None.gif
</ script >  

1000就表示了,每隔1000毫秒就更新一次aa里面的内容.

6:对WEB页面对象的常用操作,
.select()  选中对象(文本框)的内容
.focus()   让某个对象获取焦点
checked  检查单选按钮是否处于选中状态
若存的是一组单选按钮,根据document的一些方法:

getElementById     获取对 ID 标签属性为指定值的第一个对象的引用。
getElementsByName     根据 NAME 标签属性的值获取对象的集合。
getElementsByTagName     获取基于指定元素名称的对象集合。
可以用getElementsByName来解决,它是取得一些同名对象的集合,与getElementById(指定值的第一个对象的引用)不同.如下所示:

None.gif var  tmppara  =  document.getElementsByName( " para " + i);
None.gif   
var  j = 0 ;
ExpandedBlockStart.gifContractedBlock.gif   
for (;j < tmppara.length;j ++ ) dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(tmppara[j].checked)dot.gif{
InBlock.gif      values[cur_index] 
= tmppara[j].value;
InBlock.gif      cur_index
++;
ExpandedSubBlockEnd.gif     }

ExpandedBlockEnd.gif   }

7:确定下拉框的值:

None.gif < script language = " javascript " >
ExpandedBlockStart.gifContractedBlock.gif
function  test() dot.gif {
InBlock.gif 
var se = document.form1.select;
ExpandedSubBlockStart.gifContractedSubBlock.gif 
for(i=0;i<se.length;i++)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(se.options[i].value == 55)dot.gif{
InBlock.gif   se.options[i].selected
=true;
InBlock.gif   
break;
ExpandedSubBlockEnd.gif  }
    
ExpandedSubBlockEnd.gif }

InBlock.gif 
ExpandedBlockEnd.gif}

None.gif
</ script >

HTML 如下:

None.gif < form  id ="form1"  name ="form1"  method ="post"  action ="" >
None.gif    
< select  name ="select" >
None.gif    
< option  value ="11"  selected ="" > 11 </ option >
None.gif    
< option  value ="22" > 22 </ option >
None.gif    
< option  value ="33" > 33 </ option >
None.gif    
< option  value ="44" > 44 </ option >
None.gif    
< option  value ="55" > 55 </ option >
None.gif  
</ select >
None.gif
</ form >

删除其中的一个option:
se.options.remove(1);
其中参数1表示下标

8:使得某一对象不可用(可用刚改成false)
document.form1.submit2.disabled=true;
使得某一对象不可见(可见则改成visible)
document.form1.submit2.style.visibility="hidden";

9:日期以'yyyy-mm-dd'(2006-09-07 or 2006-9-17)

None.gif function  chkdate(value)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var datestr;
InBlock.gif    datestr
=value;
InBlock.gif    
var lthdatestr;
InBlock.gif    
if (datestr != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{lthdatestr= datestr.length} ;
InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{lthdatestr=0};    
InBlock.gif    
var tmpy="";
InBlock.gif    
var tmpm="";
InBlock.gif    
var tmpd="";
InBlock.gif    
var status;
InBlock.gif    status
=0;
InBlock.gif    
if ( lthdatestr== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{return (false)};            
InBlock.gif    
for (i=0;i<lthdatestr;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    if (datestr.charAt(i)== '-')
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            status
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (status>2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (false);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ((status==0&& (datestr.charAt(i)!='-'))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tmpy
=tmpy+datestr.charAt(i);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ((status==1&& (datestr.charAt(i)!='-'))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tmpm
=tmpm+datestr.charAt(i);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ((status==2&& (datestr.charAt(i)!='-'))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tmpd
=tmpd+datestr.charAt(i);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    year
=new String (tmpy);
InBlock.gif    month
=new String (tmpm);
InBlock.gif    day
=new String (tmpd);
InBlock.gif    
if ((tmpy.length!=4|| (tmpm.length>2 || tmpm.length<1|| (tmpd.length!=2))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return (false);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return (false);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if (!((year % 4)==0&& (month==2&& (day==29))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return (false);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if ((month<=7&& ((month % 2)==0&& (day>=31))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return (false);
InBlock.gif    
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if ((month>=8&& ((month % 2)==1&& (day>=31))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return (false);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if ((month==2&& (day==30))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return (false);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return (true);
ExpandedBlockEnd.gif}

再加一个比较函数,刚刚写的

ExpandedBlockStart.gif ContractedBlock.gif /**/ /************************************************************
InBlock.gif *将yyyy-mm-dd的日期型数据换成数字型以比较大小 add by zhangyinxin
ExpandedBlockEnd.gif************************************************************
*****/

ExpandedBlockStart.gifContractedBlock.gif
function  changDate2Num(date) dot.gif {
InBlock.gif    
var tmp = date.split('-') ;
InBlock.gif    
var result;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(tmp.length<=0)dot.gif{
InBlock.gif        alert('日期格式(yyyy
-mm-dd)不正确!');
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
elsedot.gif{
InBlock.gif        result 
= tmp[0];
InBlock.gif        
if(tmp[1].length<2)    tmp[1= "0"+tmp[1];
InBlock.gif        result 
+= tmp[1]; 
InBlock.gif        
if(tmp[2].length<2)    tmp[2= "0"+tmp[2];
InBlock.gif        result 
+= tmp[2]; 
ExpandedSubBlockEnd.gif    }

InBlock.gif
//    alert(result);
InBlock.gif
    return tmp;
ExpandedBlockEnd.gif}

10:对一组单选按钮的处理---是否选中or返回选中值

None.gif function  getValueofCheckBoxSelected_this(dyfs)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var controlarray = document.getElementsByName(dyfs);
InBlock.gif    
if(controlarray==null||controlarray.length==0)
InBlock.gif        
return false;
InBlock.gif       
for (var i=0; i<controlarray.length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        
if(controlarray[i].checked)
InBlock.gif            
return controlarray[i].value;    
InBlock.gif            
//如果要返回是否选中,这儿返回true啦,下面返回false    
ExpandedSubBlockEnd.gif
    }
    
InBlock.gif    
return null;
ExpandedBlockEnd.gif}

11,从A页面打开一个页面B,从B中输入相关信息后再从B页面把信息传到A页面处理:
从A页面打开B页面的方法:

ExpandedBlockStart.gif ContractedBlock.gif function  addPersonInfo() dot.gif {
InBlock.gif  
var obj =  window;
InBlock.gif  
var url = "<%=contextpath%>/jsp/addinfo.jsp";
InBlock.gif  window.showModalDialog(url,obj,'dialogwidth
=500px; dialogheight=300px;status=no; help=no');
ExpandedBlockEnd.gif}

主要是其中的obj参数起作用.然后在B页面 中返回:

None.gif dot.gif.
None.gif
var  obj  =  window.dialogArguments;
None.gif    obj.addRow(username,phone,email);
None.gif    window.close();
None.gifdot.gifdot.gif

注意这儿只取了主要部分:username,phone,emial均是在B页面取得一些信息obj.addRow()中addRow是A页面中定义的,如下:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /******************************************************************
InBlock.gif@name addRow---为******增加一行
InBlock.gif@parameters    null
InBlock.gif@retrun null
InBlock.gif@author zhangyinxin
ExpandedBlockEnd.gif*****************************************************************
*/

ExpandedBlockStart.gifContractedBlock.gif
function  addRow(name,phone,email) dot.gif {
InBlock.gif    do something......
ExpandedBlockEnd.gif}


12:得用JS在WEB页面中动态增加行列:
这里主要用到了innerHTML, cells,rows,appendChild等一些方法与属性有了它们基本上就OK了,更多可以参考HTML的参考手册,可以做到如在DW更改界面一样,感觉很爽(比如更改对象的title ,id ,style, name)!

None.gif dateTr  =  document.getElementById( " tr_name " ); // 它实际上是一列也就是<tr></tr>'tr_name'是我自定义
None.gif
    NewTr  =  document.all.dyzmx.cloneNode( true );
None.gif    NewTr.style.display 
=   " block " ;
None.gif    
var  cur_lxr_xm  =  dateTr.parentNode.rows[ 1 ].cells[ 1 ].innerHTML;
None.gif    dateTr.parentNode.appendChild(NewTr.cloneNode(
true ));
None.gif    
var  added_new_tr  =  dateTr.parentNode.lastChild;    
None.gif..

主要用到了cloneNode方法,其它更多可以参考关于HTML的对象介绍,然后再通过appendChild来增加一行(在此是增加一行).当然也可以自己定义一些元素来初始化innerHTML,as :

None.gif html  =   "  <INPUT type=text style=\ " width:30px;\ "  value= " + k + "  name=\ " rwbh\ "  maxlength=3> " ;
None.gif        start.getElementsByTagName(
" tr " )[k].firstChild.innerHTML  =  html;

顺便说一下关于删除:

None.gif var  child_len  =  dateTr.parentNode.rows.length - 1 ;
ExpandedBlockStart.gifContractedBlock.gif        
for (c2 = child_len; c2 > 0 ; c2 -- ) dot.gif {
InBlock.gif            
var rows = [];
InBlock.gif            
if(name=='lxr')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(dateTr.parentNode.rows[c2].title=='lxr_data')dot.gif{
InBlock.gif                    rows.push(dateTr.parentNode.rows[c2]);
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
for(var i=0;i<rows.length;i++)dot.gif{
InBlock.gif                        rows[i].parentNode.removeChild(rows[i]);
ExpandedSubBlockEnd.gif                    }

InBlock.gif}        
ExpandedSubBlockEnd.gif                }

注意这种方法删除时,一不小心不犯错了.一方面for(c2=child_len; c2>0; c2--)应该是递减的方式来遍历(因为rows里面push的时候,与删除的时候节点是反向的,当然假如取rows的时候反向取也OK的),另一方面 dateTr.parentNode.rows.length-1不可以直接放到for里面(因为 dateTr.parentNode.rows.length-1的长值每在for里面跑一次就变一次,当然是变小啦).

所以也可以如下处理:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /******************************************************************
InBlock.gif@name delRow---为******删除选项中行
InBlock.gif@parameters    null
InBlock.gif@retrun null
InBlock.gif@author zhangyinxin
ExpandedBlockEnd.gif*****************************************************************
*/

ExpandedBlockStart.gifContractedBlock.gif
function  delRow() dot.gif {
InBlock.gif    
var checkboxs = document.getElementsByName("checkspecial");
InBlock.gif    
var rows = [];
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for(var i=0;i<checkboxs.length;i++)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(checkboxs[i].checked && checkboxs[i].parentNode.parentNode.title!='tmp')dot.gif{
InBlock.gif            rows.push(checkboxs[i].parentNode.parentNode);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
for(var i=0;i<rows.length;i++)dot.gif{
InBlock.gif        rows[i].parentNode.removeChild(rows[i]);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

我这儿用到了复选框,当然这并不影响删除的效果实现,它不过是一种删除条件罢了(比如title也是哦)

13:第一次见到这样的自定义函数:

ExpandedBlockStart.gif ContractedBlock.gif function  $()  dot.gif {
InBlock.gif  
var elements = new Array();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
for (var i = 0; i < arguments.length; i++dot.gif{
InBlock.gif    
var element = arguments[i];
InBlock.gif    
if (typeof element == 'string')
InBlock.gif      element 
= document.getElementById(element);
InBlock.gif
InBlock.gif    
if (arguments.length == 1)
InBlock.gif      
return element;
InBlock.gif
InBlock.gif    elements.push(element);
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
return elements;
ExpandedBlockEnd.gif}

None.gif
None.gif


应用如下:

ExpandedBlockStart.gif ContractedBlock.gif this .removeRow   =    function (_id) dot.gif {  
InBlock.gif                       
var  row  =  $(this.id  +  "_row_"+_id);  
InBlock.gif                       
if(!row)return;  
InBlock.gif                       row.parentNode.removeChild(row);  
ExpandedBlockEnd.gif           }
 

感觉蛮不错!

呵呵 我一天添一些JS上来,可惜朋友们看不到,因为这"贴子"沉下去了.没办法,可是它们真的帮我不少忙,我自己的也常到这儿来取经....
...   ....

posted on 2006-03-06 17:05 Jkallen 阅读(337) 评论(2)   编辑  收藏 收藏至365Key 所属分类: AJAX
33886.html?webview=1
FeedBack:
#  re: 妙用javascript
2006-03-06 21:27 | 剑事
function checkuser(name)
{
window.open("about:blank","zhutong", 'menubar=0,width=350,height=240,left=230,top=200,resizable=0,scrollbars=auto');
window.document.forms['form1'].target = "zhutong";
window.document.forms['form1'].action = "Account/check.jsp?username="+name;
window.document.forms['form1'].submit();
}
可以改成这样
function checkuser(name)
{
window.open("Account/check.jsp?username="+name,"zhutong", 'menubar=0,width=350,height=240,left=230,top=200,resizable=0,scrollbars=auto');

}


<a href="javascript:checkuser(window.document.forms['SignUpForm'].name.value);">
<bean:message key="account.signup.input.name.check" />
</a>
这个可以改成这样
<a href="javascript:checkuser(SignUpForm.name.value);">
<bean:message key="account.signup.input.name.check" />
</a>

转载于:https://www.cnblogs.com/Nina-piaoye/archive/2006/06/29/438436.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值