📄 utility.js
字号:
//继承,来自己于prototype.js
Object.extend = function(destination, source)
{
for (property in source)
{
destination[property] = source[property];
}
return destination;
}
var Class = {
Create: function() {
return function() {
this.Initialize.apply(this, arguments);
}
}
}
//HTML节点,来自己于prototype.js
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
//得到xml的文档对象
function XmlDocument(XmlPath)
{
var xmlDocNew;
if(window.ActiveXObject)
xmlDocNew=new ActiveXObject("Microsoft.XMLDOM");
else if (document.implementation && document.implementation.createDocument)
xmlDocNew=document.implementation.createDocument('','doc',null);
xmlDocNew.async=false;
xmlDocNew.load(XmlPath);
return xmlDocNew;
}
//返回xmlHTTP xx为异步还是同步,如果是true,xml.onreadystatechange = funcMyHandler;
function CreateXmlHttp(HttpMethod,urls,xx)
{
var xml;
if(window.ActiveXObject)
xml = new ActiveXObject("Microsoft.XMLHTTP");
else if (typeof XMLHttpRequest!='undefined')
xml = new XMLHttpRequest();
xml.open(HttpMethod,urls,xx);
return xml;
}
function SetCookie(str,values){
var Then = new Date();
Then.setTime(Then.getTime() + 3600*24*265*1000 );
document.cookie = str+"="+encodeURI(values);
}
function GetCookie(str)
{
re = new RegExp(str+"=([^;]*)");
if(document.cookie=='')return '';
var tem1=document.cookie.match(re);
if(tem1)
return decodeURI(tem1[1]);
else return '';
}
//是不是IE
function IsIE(){
return /MSIE/.test(navigator.userAgent);
}
//是不是IE7
function IsIE7(){
return /MSIE 7/.test(navigator.userAgent);
}
//处理下拉框问题
var HSelect=
{
isHidden:false,
Hidden:function()
{
if(this.isHidden || IsIE7())return;
var sel=document.getElementsByTagName("Select");
var input,v='';
for(var i=0;i<sel.length;i++)
{
v=(sel[i].selectedIndex>=0) ? sel[i].options[sel[i].selectedIndex].text : ' ';
input=createElement('input',{'type':'text','class':'selectReplace'},{'width':sel[i].clientWidth-8+'px'});
input.value=v;
sel[i].parentNode.insertBefore(input,sel[i]);
sel[i].style.display="none";
}
this.isHidden=true;
AddEventListener(document,"mousedown",this.Show);
},
Show:function()
{
HSelect.isHidden=false;
var sel=document.getElementsByTagName("Select");
for(var i=0;i<sel.length;i++)
{
sel[i].parentNode.removeChild(sel[i].previousSibling);
sel[i].style.display="";
}
RemoveEventListener(document,"mousedown",HSelect.Show);
}
}
/*****************************************
函数名称:formatFloat
函数说明:截取小数点后两位数?
传入参数:进行转换的数?
返回:转换后的?
*****************************************/
function formatFloat(value)
{
value = Math.round(parseFloat(value)*100)/100;
if(value.toString().indexOf(".")<0)
value = value.toString()+".00";
return value;
}
//选择
function FocusSelect(tid)
{
document.getElementById(tid).focus();
document.getElementById(tid).select();
}
function _GET(str)
{
re = new RegExp(str+"=([^&]*)");
var getstr=window.location.search.match(re);
if(getstr)
return decodeURI(getstr[1]);
else return '';
}
Object.extend(String.prototype, {
//返回一个把所有的HTML或XML标记都移除的字符串。
stripTags: function() {
return this.replace(/<\/?[^>]+>/gi, '');
},
//返回一个把所有的script都移除的字符串。
stripScripts: function() {
return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
},
//返回一个包含在string中找到的所有<script>的数组。
extractScripts: function() {
var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
return (this.match(matchAll) || []).map(function(scriptTag) {
return (scriptTag.match(matchOne) || ['', ''])[1];
});
},
//执行在string中找到的所有<script>。
evalScripts: function() {
return this.extractScripts().map(eval);
},
//把querystring分割才一个用parameter name做index的联合Array,更像一个hash。
toQueryParams: function() {
var pairs = this.match(/^\??(.*)$/)[1].split('&');
return pairs.inject({}, function(params, pairString) {
var pair = pairString.split('=');
params[pair[0]] = pair[1];
return params;
});
},
len:function()
{
return this.replace(/[^\x00-\xff]/g,"aa").length;
},
trim:function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
});
//添加事件监听
function AddEventListener(el,eventName,fun){
if(IsIE())el.attachEvent('on'+eventName,fun);
else el.addEventListener(eventName,fun,false);
}
//删除事件监听
function RemoveEventListener(el,eventName,fun){
if(IsIE())el.detachEvent('on'+eventName,fun);
else el.removeEventListener(eventName,fun,false);
}
//获取坐标X
function GetLeft(el){
var left=el.offsetLeft;
while(el=el.offsetParent){
left+=el.offsetLeft;
}
return left;
}
//获取坐标Y
function GetTop(el){
var top=el.offsetTop;
while(el=el.offsetParent){
top+=el.offsetTop;
}
return top;
}
//获取事件
function SearchEvent(){
var _caller=SearchEvent.caller;
while(_caller){
var arg=_caller.arguments[0];
if(arg){
if(arg.constructor.toString().toLowerCase().indexOf('event')>-1){
return arg;
}
}
_caller=_caller.caller;
}
return null;
}
//标签名,属性,样式,内容
function createElement(tagName, attribute, style, word) {
var e = document.createElement(tagName);
if (attribute) {
for (var k in attribute) {
if (k == 'class') e.className = attribute[k];
else if (k == 'id') e.id = attribute[k];
else e.setAttribute(k, attribute[k]);
}
}
if (style) { for (var k in style) e.style[k] = style[k]; }
if (word) { e.innerHTML=word; }
return e;
}
//查看ID的Option是否存在v的值
function ISExistOption(id,v)
{
var option=document.getElementById(id).options;
for(var i=0;i<option.length;i++)
{
if(option[i].value==v)return true;
}
return false;
}
//删除Option所有
function DelAllOption(id,begin)
{
if(begin==null)begin=0;
var option=document.getElementById(id).options;
var l=option.length;
for(var i=begin;i<l;i++)
{
option[i]=null;
i--;
l--;
}
}
// 删除SELECT的一个节点
function DelOption(id)
{
var option=document.getElementById(id).options;
var l=option.length;
for(var i=0;i<l;i++)
{
if(option[i].selected)
{
option[i]=null;
i--;
l--;
}
}
}
function OptionEdit(id,v,t)
{
var obj=document.getElementById(id).options[document.getElementById(id).selectedIndex];
if(v!=null)obj.value=v;
if(t!=null)obj.text=t;
}
function OptionAdd(bID,eID)
{
var b=document.getElementById(bID).options;
var e=document.getElementById(eID).options;
for(var i=0;i<test.length;i++)
{
if(test[i].selected)
{
if(!ISExistOption(eID,b[i].value))e[audits.length]=new Option(b[i].text,b[i].value);
}
}
}
function GetOptionData(id)
{
var option=document.getElementById(id).options;
var re="";
for(var i=0;i<option.length;i++)
{
if(option[i].value.split(',').length>1)
{
alert('有符号“,”存在,请删除!');
return;
}
re+=option[i].value+",";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -