常用的Javascript函数

admin 发布于:2015-11-11 17:59 栏目: 浏览:553 评论:0
软件界面采用WEB界面,因此要用到Javascript.一些通用JS函数整理了下:)
/*判断浏览器版本*/
var w3c = (document.getElementById) ? true : false;
var agt = navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
var ie5 = (w3c && ie) ? true : false;
var ns6 = (w3c && (navigator.appName == "Netscape")) ? true : false;
var op8 = (navigator.userAgent.toLowerCase().indexOf("opera") == -1) ? false : true;
 
function ExChgCls(o, a, b) { //切换对象的className
    o.className = o.className == a ? b : a;
}
 
function ExChgHtml(o, a, b) { //切换对象的innerHTML
    o.innerHTML = o.innerHTML == a ? b : a;
}
 
function Oh(o, s) { //输出字符串给某个对象
    Ob(o).innerHTML = s;
}
 
function SetSrc(o, s) { //改变img、iframe等对象的src值
    Ob(o).src = s;
}
 
function Ob(o) { //取得一个对象
    return document.getElementById(o) ? document.getElementById(o) : o;
}
 
function IsSubStr(p, s) { //判断是否为子字符串
    return (p.indexOf(s) > -1);
}
 
function Hd(o) { //隐藏某个对象
    Ob(o).style.display = "none";
}
 
function Sw(o) { //显示某个对象
    Ob(o).style.display = "block";
}
 
function Sw2(o) { //显示某个对象
    Ob(o).style.display = "";
}
 
function Sf(o, s, b) {
    //该函数用于多按钮共用同一表单,其中:o为表单的ID b为隐藏的input的ID(其value供后如判断用户提交动作类型) s为用户提交动作类型
    Ob(b).value = s;
    Ob(o).submit();
}
 
/*XmlHttp*/
function Gp(url, o) { //GetHttpPage
    var o = Ob(o);
    var PageRequest = false;
    IsLoading(o);
    if (window.XMLHttpRequest) { // if Mozilla, Safari etc
        PageRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // if IE
        try {
            PageRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                PageRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {;
            }
        }
    } else {
        return false;
    }
    PageRequest.onreadystatechange = function() {
        if (PageRequest.readyState == 4) {
            Lp(PageRequest, o);
        }
    }
    PageRequest.open('GET', url, true);
    PageRequest.send(null);
}
 
function Lp(PageRequest, o) { //LoadPage
    if (PageRequest.readyState == 4 && (PageRequest.status == 200 || window.location.href.indexOf("http") == -1))
        Ob(o).innerHTML = PageRequest.responseText;
    //Hd("OpMsg");
}
 
function IsLoading(o) {
    o.innerHTML = "数据加载中,请稍候......";
    //Sw2("OpMsg");
}
游客

返回顶部