搜索
首页web前端js教程解析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(被测字串)检测一个字符串是否匹配某个模式.
}
}

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
JavaScript数据类型:浏览器和nodejs之间是否有区别?JavaScript数据类型:浏览器和nodejs之间是否有区别?May 14, 2025 am 12:15 AM

JavaScript核心数据类型在浏览器和Node.js中一致,但处理方式和额外类型有所不同。1)全局对象在浏览器中为window,在Node.js中为global。2)Node.js独有Buffer对象,用于处理二进制数据。3)性能和时间处理在两者间也有差异,需根据环境调整代码。

JavaScript评论:使用//和 / * * / * / * /JavaScript评论:使用//和 / * * / * / * /May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript:开发人员的比较分析Python vs. JavaScript:开发人员的比较分析May 09, 2025 am 12:22 AM

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

Python vs. JavaScript:选择合适的工具Python vs. JavaScript:选择合适的工具May 08, 2025 am 12:10 AM

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript:了解每个的优势Python和JavaScript:了解每个的优势May 06, 2025 am 12:15 AM

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

JavaScript的核心:它是在C还是C上构建的?JavaScript的核心:它是在C还是C上构建的?May 05, 2025 am 12:07 AM

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript应用程序:从前端到后端JavaScript应用程序:从前端到后端May 04, 2025 am 12:12 AM

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

Python vs. JavaScript:您应该学到哪种语言?Python vs. JavaScript:您应该学到哪种语言?May 03, 2025 am 12:10 AM

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具