検索
ホームページウェブフロントエンドjsチュートリアルjs共通関数 2008-8-16 仕上げページ1/2_javascriptスキル

//js常用関数数更新2008-8-16 取自网络

function $(id) {
return document.getElementById(id);
}


/**************
関数: getElementsByClassName
使用法:
ドキュメント内の「info-links」というハイパーリンク クラスを取得します。
getElementsByClassName(document, "a", "info-links");
col であるコンテナ内の p のクラスを取得します。
getElementsByClassName(document.getElementById("container"), "p" , "col");
ドキュメント内の「クリックミー」であるすべてのクラスを取得します。
getElementsByClassName(document, "*", "click-me");
配列を返します
******************/
function getElementsByClassName(oElm, strTagName, strClassName){
var arrElements = (strTagName == "*" && oElm.全て)? 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; ioElement = arrElements[i];
if(oRegExp.test(oElement.className))
arrReturnElements.push(oElement);
}
return (arrReturnElements)
}





/**************
replaceAll:
文字列内の文字を置換します。
使用法:
yourstring.replaceAll("置換する文字", "何に置換するか");
例:
"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);
}


/**************
* 文字列の前後のスペースの処理。
※途中のスペースを置換したい場合はreplaceAllメソッドを使用してください。
* 使用法:
* " cssrain ".trim();
*****************/
String.prototype.trim=function()
{
return this.replace(/(^s* )|(s*$)/g,"");//正文字列前後空格,用字符列置換。
}


/**************
* 文字列の実際の長さを計算します
//文字列には属性長がありますが、英語の文字を区別できません
//中国語の文字を計算します。および全角文字。ただし、データを保存する場合、漢字と全角文字は 2 バイトで保存されます。//すべて追加の処理が必要です。
使用法:


****************** の実際の長さを返す関数を自分で書きました。*/
String.prototype.codeLength=function(){
var len=0;
if(this==null||this.length==0)
return 0;
var str=this.replace(/(^s*)|(s*$)/g,"");//去掉空格
for(i=0;iif(str.charCodeAt(i)>0&&str.charCodeAt(i)len ;
else
len =2;
len を返します。
}


//JS获取文字列の实际長さ、文字列の長さプロパティの代わりに使用されます
String.prototype.length = function(){
return this.replace (/[u4e00-u9fa5] /g,"**").length;
}

/**************
//Filter HTML
//コメント時にユーザーが悪意のあるスクリプトを送信するのを防ぐために、まず HTML タグをフィルタリングし、二重引用符と一重引用符を除外します。引用符、記号 &、記号 使用法:


******************/
String.prototype.filterHtml=function(){
return this.replace(/&/g,"&").replace (//g,">").replace(/"/g,""").replace(/'/g,"'");
}


/**************
format:
フォーマット時間。
使用法:
yourdate.format("yyyyy-MM-dd" );
例:
obj0 = new Date("Sun May 04 2008").format("yyyy-MM-dd" ) ;
obj1 = new Date().format("yyyy-MM-dd hh:mm:ss");
obj2 = new Date().format("yyyy-MM-dd"); >obj3 = 新しい日付().format("yyyy/MM/dd");
obj4 = 新しい日付().format("MM/dd/yyyy");
****** *******/
Date.prototype.format = function(format)
{
var o = {
"M " : this.getMonth() 1, //月
"d " : this.getDate(), //日
"h " : this.getHours(), //時間
"m " : this.getMinutes(), //分
"s " : this.getSeconds(), //秒
"q " : Math.floor((this.getMonth() 3)/3), / /quant
"S" : this.getMilliseconds() //ミリ秒
}
if(/(y )/.test(format)) format=format.replace(RegExp.$1,
(this.getFull Year() "").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] :
("00" o[k]).substr(("" o[k]).length);
戻り形式;
}


/**************
format:
Format number.
Example:
var n = format_number( 123456.45656 , 2 ); // .toFixed(2) also It can be implemented, but it is not compatible with FF.
alert(n);
*****************/
function format_number(str,digit)
{
if(isNaN(str))
{
alert("您传入的值不是数字!");
return 0;
}
else if(Math.round(digit)!=digit)
{
alert("您输入的小数位数不是整数!");
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 button.
* Usage:
*
*
*
*
*******************/
function getRadioValue(radioName){
var obj=document.getElementsByName(radioName);
for(var i=0;iif(obj[i].checked){
return obj[i].value;
}
}
}

/**************
* Select all/unselect/invert checkboxes
* Usage:

************ **/
function checkAll(form, sel) {
for (i = 0, n = form.elements.length; 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 if it is selected.
* If none is selected, false will be returned.
* Usage:

*******************/
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>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;
}

//如果控件值=原来值则清空
function clearInput(input){
if(input.value == input.defaultValue){
input.value = "";
}
}

/*****************Form operation ends*************/


/**************/
//收藏到书签.(兼容IE和FF)。

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 searching in the text box.
"Search" is displayed in the text, and then when the user clicks the text with the mouse, the content of the
text box is cleared. If the user does not fill in the content, the value of the text is restored.
If filled in, it will be displayed as filled in by the user.
Usage:


*********************
函数 : 文本框得到与失去焦点 操作。
这个方法经常在文本框搜索的时候出现。
文本里显示 “ 搜索 ”,然后当用户鼠标点击此文本,
文本框内容清空。如果用户没填写内容,那么文本的值又复原。
如果填写了,就显示用户填写的。
用法:


************
Function: Used to determine whether the mouse click is left or right. (Compatible with IE and ff)
Usage:
onmousedown="mouse_keycode(event)"
*******************/
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: Trigger the onclick event of an object. (Compatible with IE and FF)
Usage:


*********************
函数 : 用来判断鼠标按的是左键还是右键。(兼容IE和ff)
用法:
onmousedown="mouse_keycode(event)"
***/
function mouse_keycode(event){
var event=event||window.event;
var nav=window.navigator.userAgent;
if (nav.indexOf("MSIE")>=1) //如果浏览器为IE.解释:因为 document.all 是 IE 的特有属性,所以通常用这个方法来判断客户端是否是IE浏览器 ,document.all?1:0;
{
if(event.button==1){alert("左键")}
else if(event.button==2){alert("右键")}
}
else if(nav.indexOf("Firefox")>=1) ////如果浏览器为Firefox
{
if(event.button==0){alert("左键");}
else if(event.button==2){alert("右键");}
}
else{ //如果浏览器为其他
alert("other");
}
}


/***
函数 :触发某个对象的onclick事件。(兼容IE和FF)
用法: ***/ 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); } } /*** 实现按回车提交 ******************/
function QuickPost(evt,form){
var evt = window.event?window.event:evt; (evt.keyCode == 13){
document.getElementById(form).submit();
}
}


/***********
番号であるかどうかを確認してください
**********/
function checkIsInteger( str)
{
//空の場合はチェックが通過します
if(str == "")
return
if(/^(- ?)(d ) $/.test(str))
true を返す;
else
false を返す
}


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
如何使用JS和百度地图实现地图平移功能如何使用JS和百度地图实现地图平移功能Nov 21, 2023 am 10:00 AM

如何使用JS和百度地图实现地图平移功能百度地图是一款广泛使用的地图服务平台,在Web开发中经常用于展示地理信息、定位等功能。本文将介绍如何使用JS和百度地图API实现地图平移功能,并提供具体的代码示例。一、准备工作使用百度地图API前,首先需要在百度地图开放平台(http://lbsyun.baidu.com/)上申请一个开发者账号,并创建一个应用。创建完成

js字符串转数组js字符串转数组Aug 03, 2023 pm 01:34 PM

js字符串转数组的方法:1、使用“split()”方法,可以根据指定的分隔符将字符串分割成数组元素;2、使用“Array.from()”方法,可以将可迭代对象或类数组对象转换成真正的数组;3、使用for循环遍历,将每个字符依次添加到数组中;4、使用“Array.split()”方法,通过调用“Array.prototype.forEach()”将一个字符串拆分成数组的快捷方式。

如何使用JS和百度地图实现地图热力图功能如何使用JS和百度地图实现地图热力图功能Nov 21, 2023 am 09:33 AM

如何使用JS和百度地图实现地图热力图功能简介:随着互联网和移动设备的迅速发展,地图成为了一种普遍的应用场景。而热力图作为一种可视化的展示方式,能够帮助我们更直观地了解数据的分布情况。本文将介绍如何使用JS和百度地图API来实现地图热力图的功能,并提供具体的代码示例。准备工作:在开始之前,你需要准备以下事项:一个百度开发者账号,并创建一个应用,获取到相应的AP

如何使用JS和百度地图实现地图多边形绘制功能如何使用JS和百度地图实现地图多边形绘制功能Nov 21, 2023 am 10:53 AM

如何使用JS和百度地图实现地图多边形绘制功能在现代网页开发中,地图应用已经成为常见的功能之一。而地图上绘制多边形,可以帮助我们将特定区域进行标记,方便用户进行查看和分析。本文将介绍如何使用JS和百度地图API实现地图多边形绘制功能,并提供具体的代码示例。首先,我们需要引入百度地图API。可以利用以下代码在HTML文件中导入百度地图API的JavaScript

js中new操作符做了哪些事情js中new操作符做了哪些事情Nov 13, 2023 pm 04:05 PM

js中new操作符做了:1、创建一个空对象,这个新对象将成为函数的实例;2、将新对象的原型链接到构造函数的原型对象,这样新对象就可以访问构造函数原型对象中定义的属性和方法;3、将构造函数的作用域赋给新对象,这样新对象就可以通过this关键字来引用构造函数中的属性和方法;4、执行构造函数中的代码,构造函数中的代码将用于初始化新对象的属性和方法;5、如果构造函数中没有返回等等。

用JavaScript模拟实现打字小游戏!用JavaScript模拟实现打字小游戏!Aug 07, 2022 am 10:34 AM

这篇文章主要为大家详细介绍了js实现打字小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

php可以读js内部的数组吗php可以读js内部的数组吗Jul 12, 2023 pm 03:41 PM

php在特定情况下可以读js内部的数组。其方法是:1、在JavaScript中,创建一个包含需要传递给PHP的数组的变量;2、使用Ajax技术将该数组发送给PHP脚本。可以使用原生的JavaScript代码或者使用基于Ajax的JavaScript库如jQuery等;3、在PHP脚本中,接收传递过来的数组数据,并进行相应的处理即可。

js是什么编程语言?js是什么编程语言?May 05, 2019 am 10:22 AM

js全称JavaScript,是一种具有函数优先的轻量级,直译式、解释型或即时编译型的高级编程语言,是一种属于网络的高级脚本语言;JavaScript基于原型编程、多范式的动态脚本语言,并且支持面向对象、命令式和声明式,如函数式编程。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター