JS Common function Update 2008-8-16 from the network
function $ (ID) {
return document.getElementById (ID);
}
/**************
Function: Getelementsbyclassname
How to use:
Gets the hyperlink class within the document "Info-links".
Getelementsbyclassname (document, "A", "info-links");
The class that gets the div within the container is Col.
Getelementsbyclassname (document.getElementById ("container"), "div", "col");
Get all the classes within the document to be "Click-me".
Getelementsbyclassname (document, "*", "click-me");
Returns an array
**************/
function Getelementsbyclassname (Oelm, Strtagname, strClassName) {
var arrelements = (Strtagname = = "*" && oelm.all)? OElm.all:oElm.getElementsByTagName (Strtagname);
var arrreturnelements = new Array ();
strClassName = Strclassname.replace (/-/g, "\-");
var oregexp = new RegExp ("(^|\s)" + strClassName + "(\s|$)");
var oelement;
for (var i=0; i<arrelements.length; i++) {
Oelement = Arrelements[i];
if (Oregexp.test (oelement.classname))
Arrreturnelements.push (oelement);
}
Return (arrreturnelements)
}
/**************
ReplaceAll:
Replaces characters in a string.
Usage:
Yourstring.replaceall ("the character to be replaced", "what to replace");
Example:
"Cssrain". ReplaceAll ("s", "a");
"CS SR ai n". ReplaceAll ("", "");
**************/
String.prototype.replaceAll = function (afindtext,areptext) {
Raregexp = new RegExp (Afindtext, "G");
Return This.replace (Raregexp,areptext);
}
/**************
* String before and after space processing.
* If you want to replace the middle space, please use the ReplaceAll method.
Usage
* "Cssrain". Trim ();
**************/
String.prototype.trim=function ()
{
Return This.replace (/(^\s*) | ( \s*$)/g, ""), or a space before and after the string, replaced with an empty string.
}
/**************
* Calculate the true length of the string
String has an attribute length, but it cannot differentiate between English characters,
Calculates Chinese and full-width characters. But at the time of data storage, Chinese and full-width are stored in two bytes,
All need extra processing. Wrote a function that returns the length of the string being true.
Usage:
<input type= "text" Name= "Rain" id= "Rain"/>
<input type= "button" id= "Test" value= "test" onclick= "alert (document.getElementById (' rain '). Value.codelength ())" />
**************/
String.prototype.codelength=function () {
var len=0;
if (this==null| | this.length==0)
return 0;
var str=this.replace (/(^\s*) | ( \s*$)/g, "");//Remove space
for (i=0;i<str.length;i++)
if (Str.charcodeat (i) >0&&str.charcodeat (i) <128)
len++;
Else
len+=2;
return Len;
}
JS gets the actual length of the string, which replaces the string's length property
String.prototype.length = function () {
Return This.replace (/[\u4e00-\u9fa5]+/g, "* *"). Length;
}
/**************
Filtering HTML
In the comments to prevent users from submitting malicious script, you can first filter the HTML tags, filter out the double quotes, single quotes, symbols, symbols
Usage:
<input type= "text" Name= "Rain" id= "Rain"/>
<input type= "button" id= "Test" value= "test" onclick= "alert (document.getElementById (' rain '). value.filterhtml ())" />
**************/
String.prototype.filterhtml=function () {
Return This.replace (/&/g, "&"). Replace (/</g, "<"). Replace (/>/g, ">"). Replace (/"/g," ""). Replace (/'/g, ' "");
}
/**************
Format
Format time.
Usage:
Yourdate.format ("Your date format");
Example:
Obj0 = new Date ("Sun May 2008"). Format ("Yyyy-mm-dd");
Obj1 = new Date (). Format ("Yyyy-mm-dd hh:mm:ss");
Obj2 = new Date (). Format ("Yyyy-mm-dd");
Obj3 = new Date (). Format ("Yyyy/mm/dd");
Obj4 = new Date (). Format ("mm/dd/yyyy");
**************/
Date.prototype.format = function (format)
{
var o = {
"m+": This.getmonth () +1,//month
"D+": This.getdate (),//day
"h+": this.gethours (),//hour
"m+": This.getminutes (),//minute
"S+": This.getseconds (),//second
"q+": Math.floor (This.getmonth () +3)/3),//quarter
"S": This.getmilliseconds ()//millisecond
}
if (/(y+)/.test (format)) Format=format.replace (regexp.$1,
(This.getfullyear () + ""). substr (4-regexp.$1.length));
For (var k in O) if (new RegExp ("+ K +")). Test (format)
Format = Format.replace (regexp.$1,
Regexp.$1.length==1? O[K]:
("+ o[k]"). substr (("" + o[k]). length);
return format;
}
/**************
Format
Format the number.
Example:
var n = format_number (123456.45656, 2); . toFixed (2) can also be implemented, but is not compatible with FF.
alert (n);
**************/
function Format_number (str,digit)
{
if (isNaN (str))
{
Alert ("The value you passed in is not a number!") ");
return 0;
}
else if (math.round (digit)!=digit)
{
Alert ("The number of decimal places you entered is not an integer!") ");
return 0;
}
Else
Return Math.Round (parsefloat (str) *MATH.POW (10,digit))/math.pow (10,digit);
}
/********** form Operation *********/
/**************
* Get the selected value of the Radio box.
Usage
*<input type= "Radio" value= "1" name= "Cssrain"/>
*<input type= "Radio" value= "2" name= "Cssrain" checked/>
*<input type= "Radio" value= "3" name= "Cssrain"/>
*<input type= "button" onclick= "Alert (getradiovalue (' Cssrain '))" value= "test"/>
**************/
function Getradiovalue (radioname) {
var obj=document.getelementsbyname (radioname);
for (Var i=0;i<obj.length;i++) {
if (obj[i].checked) {
return obj[i].value;
}
}
}
/**************
* check box Select/Uncheck/reverse
Usage
<form id= "Form_a" >
<input type= "checkbox" value= "1" name= "a"/>
<input type= "checkbox" value= "2" name= "a" checked/>
<input type= "checkbox" value= "3" name= "a"/>
<input type= "button" value= "onclick=" Checkall (' document.getElementById '), ' all ' form_a
<input type= "button" value= "onclick=" Checkall (document.getElementById (' form_a '), ' none ') "/>
<input type= "button" value= "onclick=" Checkall (document.getElementById (' form_a '), ' ")/>
</form>
**************/
function Checkall (form, sel) {
for (i = 0, n = form.elements.length i < n; i++) {
if (Form.elements[i].type = = "checkbox") {
if (form.elements[i].checked = = True) {
form.elements[i].checked = (sel = "All"? true:false);
} else {
form.elements[i].checked = (sel = "None" False:true);
}
}
}
}
/**************
* check box to check whether it is selected.
* Returns False if none is selected.
Usage
<form id= "Form_a" name= "Form_a" >
<input type= "checkbox" value= "1" name= "a"/>
<input type= "checkbox" value= "2" name= "a" checked/>
<input type= "checkbox" value= "3" name= "a"/>
<input type= "button" value= "select" onclick= "Alert (Scheckbox (' form_a ', ' a '))"/>
</form>
**************/
function Scheckbox (_formname,_checkboxname) {
var Selflag = {' checked ': 0, ' cvalues ': []};
_scheckbox = eval (' document. ') +_formname+ '. ' +_checkboxname);
if (_scheckbox) {
if (eval (_scheckbox.length)) {
for (i=0;i<_scheckbox.length;i++) {
if (_scheckbox[i].checked) {
selflag.checked++;
Selflag.cvalues.push (_scheckbox[i].value);
}
};
}else if (_scheckbox.checked) {
selflag.checked++;
Selflag.cvalues.push (_scheckbox.value);
}
if (selflag.checked) {
return selflag;
}
}
return false;
}
Empty if the control value = original value
function Clearinput (input) {
if (Input.value = = Input.defaultvalue) {
Input.value = "";
}
}
/*************** form Operation End **********/
/**************/
Bookmark is collected. (IE and FF compatible).
function Addbookmark (title,url) {
if (Window.sidebar) {
Window.sidebar.addPanel (title, URL, "");
else if (document.all) {
Window.external.AddFavorite (URL, title);
else if (Window.opera && window.print) {
return true;
}
}
/**************
function: text box gets and loses focus operation.
This method often appears when a text box is searched.
The text shows "search", and then when the user clicks on the text,
The text box contents are empty. If the user does not fill in the content, then the value of the text is restored.
If you fill out, it will show the user filled out.
Usage:
<input type= "value=" keyword search "name=" a "onfocus=" cleartxt (' a ', ' keyword search ') "onblur=" Filltxt (' a ', ' keyword search ') "/>
<input type= "text" value= "test" name= "test"/>
**************/
function Cleartxt (id,txt) {
if (document.getElementById (id). Value = = txt)
document.getElementById (ID). value= "";
return;
}
function Filltxt (id,txt) {
if (document.getElementById (id). Value = = "")
document.getElementById (ID). value=txt;
return;
}
/**************
Function: Used to determine whether the mouse is pressing the left or right button. (IE and FF compatible)
Usage:
onmousedown= "Mouse_keycode (event)"
**************/
function Mouse_keycode (event) {
var event=event| | window.event;
var nav=window.navigator.useragent;
if (Nav.indexof ("MSIE") >=1)//If the browser is ie. explanation: Because document.all is the unique property of IE, this method is usually used to determine whether the client is IE browser, document.all?1:0;
{
if (event.button==1) {alert (left key)}
else if (event.button==2) {alert (right key)}
}
else if (Nav.indexof ("Firefox") >=1)////If the browser is Firefox
{
if (event.button==0) {alert ("left Key");}
else if (event.button==2) {alert ("right Key");}
}
else{//If the browser for other
Alert ("other");
}
}
/**************
Function: The onclick event that triggers an object. (IE and FF compatible)
Usage:
<input type= "button" value= "AAA" id= "a" onclick= "alert (' Cssrain ')"/>
<input type= button "value=" triggers an onclick event with ID a "onclick=" Handertoclick (' a ')/>
**************/
function Handertoclick (ObjID) {
var Obj=document.getelementbyid (ObjID);
if (document.all) {
Obj.fireevent ("onclick");
}else{
var e=document.createevent (' MouseEvent ');
E.initevent (' click ', False,false);
Obj.dispatchevent (e);
}
}
/**************
Implement press RETURN submit
**************/
function Quickpost (evt,form) {
var evt = window.event?window.event:evt;
if (Evt.keycode = = 13) {
document.getElementById (Form). Submit ();
}
}
/*********
Verify that you are a number
**********/
function Checkisinteger (str)
{
If empty, the checksum is passed
if (str = "")
return true;
if (/^ (\-?) (\d+) $/.test (str))
return true;
Else
return false;
}
Current 1/2 page
12 Next read the full text