妙用javascript(1)

1:在一带到复杂的FRAME页面中OPEN一个窗体来(当然不用什么parent.parent啦)

  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();        
    }


以下的LINK来触发

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


当然下面的不能少啦!

</ html:form >
< form  name ="form1"  method ="post"  action ="" >
</ form >


2:关闭一当前窗口,并且无关闭提示(这个功能太常用了)

< input  type ="submit"  name ="Submit"  value ="知道啦"  onClick ='window.opener = "xxx" ;
    window.close();'
>


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

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


则可以:

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


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


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

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


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

var  curCourseID  =  pValue[c_courseID];


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

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

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

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

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

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

var  tmppara  =  document.getElementsByName( " para " + i);
   
var  j = 0 ;
   
for (;j < tmppara.length;j ++ ) {
    
if(tmppara[j].checked){
      values[cur_index] 
= tmppara[j].value;
      cur_index
++;
     }

   }

7:确定下拉框的值:

< script language = " javascript " >
function  test() {
 
var se = document.form1.select;
 
for(i=0;i<se.length;i++){
  
if(se.options[i].value == 55){
   se.options[i].selected
=true;
   
break;
  }
    
 }

 
}

</ script >

HTML 如下:

< form  id ="form1"  name ="form1"  method ="post"  action ="" >
    
< select  name ="select" >
    
< option  value ="11"  selected ="" > 11 </ option >
    
< option  value ="22" > 22 </ option >
    
< option  value ="33" > 33 </ option >
    
< option  value ="44" > 44 </ option >
    
< option  value ="55" > 55 </ option >
  
</ select >
</ 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)

function  chkdate(value)
{
    
var datestr;
    datestr
=value;
    
var lthdatestr;
    
if (datestr != "")
        
{lthdatestr= datestr.length} ;
    
else
        
{lthdatestr=0};    
    
var tmpy="";
    
var tmpm="";
    
var tmpd="";
    
var status;
    status
=0;
    
if ( lthdatestr== 0)
        
{return (false)};            
    
for (i=0;i<lthdatestr;i++)
    
{    if (datestr.charAt(i)== '-')
        
{
            status
++;
        }

        
if (status>2)
        
{
            
return (false);
        }

        
if ((status==0&& (datestr.charAt(i)!='-'))
        
{
            tmpy
=tmpy+datestr.charAt(i);
        }

        
if ((status==1&& (datestr.charAt(i)!='-'))
        
{
            tmpm
=tmpm+datestr.charAt(i);
        }

        
if ((status==2&& (datestr.charAt(i)!='-'))
        
{
            tmpd
=tmpd+datestr.charAt(i);
        }


    }

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

    
if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
    
{
        
return (false);
    }

    
if (!((year % 4)==0&& (month==2&& (day==29))
    
{
        
return (false);
    }

    
if ((month<=7&& ((month % 2)==0&& (day>=31))
    
{
        
return (false);
    
    }

    
if ((month>=8&& ((month % 2)==1&& (day>=31))
    
{
        
return (false);
    }

    
if ((month==2&& (day==30))
    
{
        
return (false);
    }

    
return (true);
}

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

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

function  changDate2Num(date) {
    
var tmp = date.split('-') ;
    
var result;
    
if(tmp.length<=0){
        alert('日期格式(yyyy
-mm-dd)不正确!');
    }
else{
        result 
= tmp[0];
        
if(tmp[1].length<2)    tmp[1= "0"+tmp[1];
        result 
+= tmp[1]; 
        
if(tmp[2].length<2)    tmp[2= "0"+tmp[2];
        result 
+= tmp[2]; 
    }

//    alert(result);
    return tmp;
}

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

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

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

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

 

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

.
var obj =
 window.dialogArguments;
    obj.addRow(username,phone,email);
    window.close();

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

/**/ /******************************************************************
@name addRow---为******增加一行
@parameters    null
@retrun null
@author zhangyinxin
*****************************************************************
*/

function addRow(name,phone,email) {
    do something......
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值