Home Web Front-end JS Tutorial js code set (friends who are learning js can take a look)_javascript skills

js code set (friends who are learning js can take a look)_javascript skills

May 16, 2016 pm 06:49 PM
js

/**
* @author Super Sha
* QQ:770104121
* E-Mail:supei_shafeng@163.com
* publish date: 2009-3-27
* All Rights Reserved
*/

var JsHelper={}; //Declare a JsHelper root namespace
JsHelper.DOM = {}; //Declare the DOM namespace under the JsHelper directory
JsHelper.Event={}; //Declare the Event event namespace in the JsHelper directory
JsHelper.Browser={}; //Declare the browser-related functions in the JsHelper directory Browser namespace
JsHelper. Ajax={}; //Declare the Ajax-related function namespace in the Jshelper directory
JsHelper.String={}; //Declare the String-related namespace in the JsHelper directory

/*
* $() can input multiple parameters and will return an array of objects
*/
var $=function(){ //Simplified version of the $ method
var elements=new Array ();
if(arguments.length==0){ //If the parameter is empty, return the document element
return document;
}
for (var i = 0; i {
var element = arguments[i];
if (typeof element == string )
{
element = document.getElementById(element);
}
if (arguments.length == 1)
{
return element;
}
elements.push(element);
}
return elements;
}
JsHelper.DOM.$=function()
{
var elements=new Array();
for (var i = 0; i {
var element = arguments[i];
if (typeof element == string )
{
element = document.getElementById(element);
}
if (arguments.length == 1)
{
return element;
}
elements.push(element);
}
return elements;
}
/*
* $Value() can input multiple parameters and will return a Value array of obtained objects
*/
JsHelper.DOM.value=function()
{
var values=new Array() ;
for (var i = 0; i {
var element = arguments[i];
if (typeof(element) == string )
{
var v=document.getElementById(element).value;
}
if(arguments.length==1)
{
return v;
}
values .push(v);
}
return values;
}
/*
makeArray generates an array return for the input parameters. If the parameter is empty, it returns "undefined", otherwise it returns Array
*/
JsHelper.String.makeArray=function()
{
var values=new Array();
if(arguments.length>0){
for ( var i = 0; i {
var element=arguments[i];
if(typeof element == "string")
{
values. push(element);
}
}
}
else
{
return "undefined";
}
return values;
}
/*
* Declare a StringBuilder class to handle connection string performance issues
*/
JsHelper.String.StringBulider={
_strs:new Array(),
append: function( str){ //Add string to attribute _strs
this._strs.push(str);
return this;
},
toString:function(){
if (arguments .length != 0) {
return this._strs.join(arguments[0]); //Return the string after combining the attribute _strs, accepting an optional parameter for the join parameter
} else{
return this._strs.join("");
}
}
};

/*
* $TagName() enters a parameter and will Returns an array of Elements TagNeme objects
*/
JsHelper.DOM.tagName=function()
{
var element=arguments[0];
if(typeof element== string )
{
var tagname=document.getElementsByTagName(element);
}
return tagname;
}

//========== ==================================================
/*
* label: HTML Label ID
* Can only be used with responseText
* Can only be used with GET method
*/
var _xmlhttp;//Declare the global XMLHttpRequest object Example
function Ajax(method, url, label){
this.method = method;
this.url = url;
try {
_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP" );
}
catch (e) {
try {
_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (s) {
_xmlhttp = new XMLHttpRequest();
}
}
Ajax.prototype.ResponseText = function(){
_xmlhttp.onreadystatechange = this.onComplete;
_xmlhttp.open(this.method, this .url, true)
_xmlhttp.send(null);
}
Ajax.prototype.onComplete = function(){
if (_xmlhttp.readyState == 4) {
if ( _xmlhttp.status == 200) {
$(label).innerHTML = _xmlhttp.responseText;
}
}
}
this.ResponseText();
}
//================================================

/*
* Determine browser type
*/
var ua = navigator.userAgent.toLowerCase();
if (window.ActiveXObject) {
JsHelper. Browser.IE = ua.match(/msie ([d.] )/)[1];
}
else if (document.getBoxObjectFor) {
JsHelper.Browser.Firefox = ua.match( /firefox/([d.] )/)[1];
}
else if (window.MessageEvent && !document.getBoxObjectFor) {
JsHelper.Browser.Chrome = ua.match(/chrome /([d.] )/)[1];
}
else if (window.opera) {
JsHelper.Browser.Opera = ua.match(/opera.([d.] ) /)[1];
}
else if (window.openDatabase) {
JsHelper.Browser.Safari = ua.match(/version/([d.] )/)[1];
}
/*
* Declare an instance of XMLHttpRequest object and return the instance
*/
JsHelper.Ajax.createRequest=function()
{
var xmlhttp=null;
try
{
xmlhttp=new XMLHttpRequest();
}
catch(trymicrosoft){
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(othermicrosoft){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlhttp;
}

/*
* A general AddEventListener function, obj is a DOM element
*/
JsHelper.Event.addEventlistener=function(labelID,eventMode,fn)
{
var obj=JsHelper.DOM. $(labelID);
if (typeof window.addEventListener != undefined ) {
obj.addEventListener(eventMode, fn, false);
}
else
if (typeof document.addEventListener != undefined ) {
obj.addEventListener(eventMode, fn, false);
}
else
if (typeof window.attachEvent != undefined ) {
obj.attachEvent("on " eventMode, fn);
}
else {
return false;
}
return true;
}

/*
* contains a Douglas Crockford's extension of function method. The copyright of the following three functions belongs to Douglas Crockford. Hereby declare
*/
Function.prototype.method = function (name, func) {
this.prototype[ name] = func;
return this;
};
Function.method( inherits , function (parent) {
var d = {}, p = (this.prototype = new parent() );
this.method( base , function uber(name) {
if (!(name in d)) {
d[name] = 0;
}
var f, r, t = d[name], v = parent.prototype;
if (t) {
while (t) {
v = v.constructor.prototype;
t -= 1;
}
f = v[name];
} else {
f = p[name];
if (f == this[name]) {
f = v [name];
}
}
d[name] = 1;
r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
d[name] -= 1;
return r;
});
return this;
});
Function.method( swiss , function (parent) {
for (var i = 1; i var name = arguments[i];
this.prototype[name] = parent.prototype[name];
}
return this;
});

/*
* A solution to the problem that IE does not support HTMLElement
*/
var DOMElement ={
extend : function(name,fn)
{
if(!document.all)
{
eval("HTMLElement.prototype." name " = fn");
}
else
{
var _createElement = document.createElement;
document.createElement = function(tag)
{
var _elem = _createElement(tag);
eval("_elem." name " = fn");
return _elem;
}
var _getElementById = document.getElementById;
document.getElementById = function(id)
{
var _elem = _getElementById( id);
eval("_elem." name " = fn");
return _elem;
}
var _getElementsByTagName = document.getElementsByTagName;
document.getElementsByTagName = function(tag)
{
var _arr = _getElementsByTagName(tag);
for(var _elem=0;_elem<_arr.length>eval("_arr[_elem]." name " = fn" );
return _arr;
}
}
}
};
/*
* The following is modeled after several DOM query functions of John Resig, the father of jQuery Capabilities
*/
DOMElement.extend("previous",function(){ // similar to previousSibling DOM Function
var elem=this;
do{
elem=elem.previousSibling ;
}while(elem&&elem.nodeType!=1);
return elem;
});

DOMElement.extend("next",function(){ //similar to nextSibling DOm Function
var elem=this;
do{
elem=elem.nextSibling;
}while(elem&&elem.nodeType!=1);
return elem;
});
DOMElement.extend("first",function(num){ //similar to firstChild DOM Function, same as parent
var elem=this;
num=num||1;
for (var i = 0; i elem = elem.firstChild;
}
return (elem && elem.nodeType!=1 ? next(elem):elem);
} );
DOMElement.extend("last",function(num){ //similar to lastChild DOM Function, same as parent
var elem=this;
num=num||1;
for (var i = 0; i elem = elem.lastChild;
}
return (elem && elem.nodeType!=1 ? prev(elem):elem);
});
DOMElement.extend("parent",function(num){ //You can return several num levels of parentNode, for example: parent(2) is equivalent to elem.parent().parent();
var elem=this;
num=num ||1;
for (var i = 0; i if (elem != null) {
elem = elem.parentNode;
}
}
return elem;
});
DOMElement.extend("hasChilds",function(){ //Judge when there are child nodes
if(this!=null && this.hasChildNodes()){
return true;
}
else{
return false;
}
});

DOMElement.extend("text",function(){ //Get the text in the label. If the parameter is not zero, you can set the text version in the label. It is also suitable for input labels
try{ //Solution Solution for Firefox not supporting InnerText
HTMLElement.prototype.__defineGetter__("innerText",function(){
var anyString = "";
var childS = this.childNodes;
for(var i =0; iif (childS[i].nodeType == 1) {
anyString = childS[i].tagName == "BR" ? "n : childS[ i].innerText;
}
else if(childS[i].nodeType == 3) {
anyString = childS[i].nodeValue;
}
}
return anyString;
});
}
catch(e){}
if (arguments.length == 1) {
if (this.innerText) {
this.innerText = arguments[0];
}
else {
this.value = arguments[0];
}
}
else {
return this.innerText || this.value;
}
});
DOMElement.extend("html",function(){ //Get the innerHTML of the element. If the parameter is not zero, you can set the text and child nodes within the element
if(arguments.length= =0){
return this.innerHTML;
}
else if(arguments.length==1)
{
this.innerHTML=arguments[0];
}
});

/*
* The following are the operations of className
*/
DOMElement.extend("getClassName",function(){ //Return element className
if(this!=null&&this.nodeType==1){
return this.className.replace(/s /, ).split( );
}
return null;
});
DOMElement.extend("hasClassName",function(){ //Determine whether there is a class class
if(this!=null&&this.nodeType==1){
var classes=this.getClassName();
for(var i=0;iif(arguments[0]==classes[i]) return true;
}
}else{
return false;
}
});

DOMElement.extend("addClass",function(){ //Add classes to elements, you can add multiple classes at once
if( this!=null&&this.nodeType==1){
for (var i = 0; i this.className = (this.className ? : ) arguments[i];
}
return this;
}
return null;
});
DOMElement.extend("removeClass",function(){ //Remove the class, if there are no parameters, then Delete all classes
if (this != null && this.nodeType == 1) {
if (arguments.length == 0) {
this.className = "";
}
else if(arguments.length!=0) {
var classes=this.getClassName();
for (var i = 0; i for (var j = 0; j if (arguments[i]==classes[j]) {
classes = classes.join(" ").replace(arguments[i], ).split(" ");
}
}
}
this.className=classes.join(" ");
}
return this;
}
return null;
});
JsHelper.__toggleflag=false; //Add a judgment switch
DOMElement.extend("toggleClass",function(classname){ //Function called by two clicks Different
if(this!=null && this.nodeType==1){
this.onclick=function(){
if(JsHelper.__toggleflag==false){
this.addClass( classname);
JsHelper.__toggleflag = true;
}else if (JsHelper.__toggleflag == true) {
this.removeClass(classname);
JsHelper.__toggleflag = false;
}
}
}
});
/*
* Add a click method to each object, similar to how jQuery's click method is used
*/
DOMElement. extend("click",function(){
if(this!=null && this.nodeType==1){
if(arguments.length==0){
alert("you have done nothing when you clicked.");
}else{
this.onclick=arguments[0];
}
}
});

/*
* Extend the hover method to each object. This method accepts two functions as parameters
*/
DOMElement.extend("hover",function(){
if(this!=null && this. nodeType==1){
if(arguments.length!=2){
alert("Require two function to be param.");
}else{
this.onmouseover=arguments[ 0];
this.onmouseout=arguments[1];
}
}
});
/*
* Function to add events to each element
* /
DOMElement.extend("addEvent",function(eventtype,fn){
if(document.all){
this.attachEvent("on" eventtype,fn);
}else{
this.addEventListener(eventtype,fn,false);
}
});
/*
* Extend the css method for each element, accepting an attribute and attribute value as parameters
*/
DOMElement.extend("css",function(){
if(this!=null && this.nodeType==1){
if(arguments.length!=2){
alert("Require two function to be param.");
}else{
this.style[arguments[0]]=arguments[1]; //Set the value of the relevant style attribute
return this;
}
}
return null;
});
/*
* //Find and return all elements that exist in a certain class, name is className, type is the HTML tag type
*/
var hasClass = function(name,type){
var r = new Array();
//var re = new RegExp(name," g");
var e=document.getElementsByTagName(type||"*");
for(var i=0;ivar classes=e[i ].getClassName();
for (var j = 0; j if (name== classes[j]) {
r.push(e[i] );
}
}
}
return r;
}
/*
* Returns a collection of specific child element references of an element. If there is no element, call For this method, the default is document
*/
DOMElement.extend("find",function(){
var elem=this||document;
var r=new Array();
if(elem!=null && (elem.nodeType==1||elem.nodeType==9)){
var e=elem.getElementsByTagName(arguments[0]);
for(var i =0;ir.push(e[i]);
}
return r;
}
return null;
}) ;

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement map heat map function How to use JS and Baidu Maps to implement map heat map function Nov 21, 2023 am 09:33 AM

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement map polygon drawing function How to use JS and Baidu Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file

See all articles