您好,欢迎访问一九零五行业门户网

解析javascript 实用函数的使用详解_javascript技巧

复制代码 代码如下:
function addbookmark(url, title){
//将网页添加到收藏addbookmark(window.location,document.title)
    if(document.all){
      window.external.addfavorite(url,title);
    }else if (window.sidebar){
      window.sidebar.addpanel(title,url,'');
    }else{
      alert('添加失败');
    }
}  
function sethomepage(obj,url) {
// sethome(this, window.location)
    try {  
        obj.style.behavior = 'url(#default#homepage)';  
        obj.sethomepage(url);  
    } catch (e) {  
        if (window.netscape) {  
            try {  
                netscape.security.privilegemanager.enableprivilege(universalxpconnect);  
            } catch (e) {  
                alert('此操作被浏览器拒绝!\n请在浏览器地址栏输入about:config并回车然后将 [signed.applets.codebase_principal_support]的值设置为true,双击即可。');  
            }  
            var prefs = components.classes['@mozilla.org/preferences-service;1'].getservice(components.interfaces.nsiprefbranch);  
            prefs.setcharpref('browser.startup.homepage', vrl);  
        }  
    }  
}  
function boxmove(box){
//可移动的层
    var w = box.scrollwidth,h = box.scrollheight;
    var iwidth = document.documentelement.clientwidth;
    var iheight = document.documentelement.clientheight;    
    var movex = 0,movey = 0,movetop = 0,moveleft = 0,moveable = false;
    box.onmousedown = function(e){   
    moveable = true;    
    e = window.event?window.event:e;
    movex = e.clientx-box.offsetleft;       
    movey = e.clienty-box.offsettop;
    box.style.zindex++;
    }
    document.onmousemove = function(e){
        if(moveable){
            e = window.event?window.event:e;       
            var x = e.clientx - movex;
            var y = e.clienty - movey;
            if ( x > 0 &&( x + w 0 && (y + h                 box.style.left = x + px;
                box.style.top = y + px;
                box.style.margin = auto;
            }
        }
    }
    document.onmouseup = function (){moveable = false;};
}
function gethtmlroot(){
/*获得html文档的根元素*/
    if(document.compatmode.tolowercase()==css1compat){
        htmlroot = document.documentelement;
    }else{
        htmlroot = document.body;
    }
    return htmlroot;
}
function showwindow(boxid, closeid, showbg){//弹窗
    htmlroot = gethtmlroot();
    box = document.getelementbyid(boxid);
    boxid = '#' + boxid;
    closeid = '#' + closeid;
    showbox = $(boxid);
    showbox.show();
    z_index = 20;
    moveleft =  (htmlroot.clientwidth- box.clientwidth)/2 + htmlroot.scrollleft +'px';
    movetop = (htmlroot.clientheight - box.clientheight)/2 + htmlroot.scrolltop;+'px';
    showbox.css({position:'absolute', left:moveleft,zindex:z_index, top:movetop});
    boxmove(box);
if(showbg){
        objmask = document.createelement(div);
        objmask.classname = boxmask;
        htmlroot.appendchild(objmask);
        objmask.style.csstext += 'position:absolute;top:0; left:0;filter:alpha(opacity=50);opacity:0.5;background:#aaa;';
objmask.style.zindex = z_index -1;
        objmask.style.width =  htmlroot.clientwidth + 'px';
        objmask.style.height = htmlroot.scrollheight + htmlroot.scrolltop + 'px';
    }
$(closeid).click(function(){
        showbox.hide();
        mybg.style.display = none;
    });
    $('.closebtn').click = function(){
        showbox.hide();
        mybg.style.display = none;   
    };
}
function getformquery(formid){
/*生成查询字串*/
    formobj = document.getelementbyid(formid);
       var i, querystring = , and = , itemvalue;
       for(i = 0; i             var item = formobj[i];
              if ( item.name!='' ){
                     if(item.type == 'select-one'){
                         itemvalue = item.options[item.selectedindex].value;
                     }else if ( item.type=='checkbox' || item.type=='radio'){
                         if ( item.checked == false ){ continue; }
                         itemvalue = item.value;
                     }else if ( item.type == 'button' || item.type == 'submit' || item.type == 'reset' || item.type == 'image'){
                         continue;
                     }else{
                          itemvalue = item.value;
                     }
                   //  itemvalue = escape(itemvalue);
                     querystring += and + item.name + '=' + itemvalue;
                     and=&;
                   //querystring += and + encodeuricomponent(item.name) + '=' +encodeuricomponent( itemvalue);
              }
       }
       return querystring;
}
//定义js错误处理函数
onerror = errhandle;
function errhandle(msg,url,line){
    var txt=
    txt = 本页中有错误!\n
    txt += 错误: + msg +\n
    txt += 地址: + url + \n
    txt += 行数: + line + \n\n
    alert(txt);
    return false;
}
function setautowidth(id,width,size){
//最小或最大宽度
    var obj = document.getelementbyid(id);
    if(size=='max'){
        obj.style.width = (obj.clientwidth > width) ? width + px : auto;
    }else{
        obj.style.width = (obj.clientwidth     }
}
function fontscroll(id){
/*向左滚动的文字 */
    var obj = document.getelementbyid(id);
    var text = obj.innerhtml;
    var first = text.charat(0);
    var left = text.substring(1, text.length);
    obj.inerhtml = left + first;
    //这个写函数之外 setinterval('fontscroll(id)', 500);
}
function bubblesort(arr){
/*冒泡排序法*/
var sign = false // 初始化换位标记为假
for(var i=0; i     for(varj=0; j         if(arr[j]> arr[j+1]){
            vartemp = arr[j]
            arr[j]= arr[j+1]
            arr[j+1]= temp
            sign= true // 若当前这一圈比较中,有过换位,则置换位标记为真
        }
    }
    if(sign)// 判断当前这一圈有过换位否…
       sign= false // 有过换位,则重置换位标记为假
    else
       break// 没有,则终止
    }
return arr;
}
function getcoordinate(evt){
/*获得光标的坐标*/
    var x = evt.clientx;
    var y = evt.clienty;
    document.getelementbyid('show').innerhtml = x +' &' + y;
}
function checkemail(email){
/*检测多个以分号(;)分隔的email格式*/
if(email != null){
    if(email.indexof(;,0) == -1){ //indexof(被查找,起始位)返某个字符串在源字符串中首次出现的位置,失败返回-1
        if(!isemail(email)){
            alert(单个邮件格式有误,请重输!);
            document.getelementbyid(email).focus();
            return false;
        }
    }else{
        var emailarr = email.split(;);//split(分隔段,数组总长度)字符串分割成字符串数组
        var i, imax = emailarr.length;
        for(i = 0; i             if(emailarr[i] != null || emailarr != ){
                if(!isemail(emailarr[i])){
                    alert(多个邮箱格式中有邮箱格式不正确,请重新核对后再输入!);   
                    document.getelementbyid(email).focus();
                    return false;
                }
            }
        }
    }   
}
function isemail(str){
   var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
   return reg.test(str);//test(被测字串)检测一个字符串是否匹配某个模式.
}
}
其它类似信息

推荐信息