自己常用的JS合在一起方便查找
记下自己常写的JS,用的时候就直接Copy,方便快捷。
No.1:input focus(base jq)
$(function(){
$("input[@type='text']").focus(function(){
$(this).addClass("focus");
}).blur(function(){
$(this).removeClass("focus");
});
});
$("input[@type='text']").focus(function(){
$(this).addClass("focus");
}).blur(function(){
$(this).removeClass("focus");
});
});
No.2:Trim() ,Ltrim(),Rtrim()
String.prototype.Trim=function()
{
return this.replace(/(^\s*)|(\s*$)/g,"");
}
String.prototype.Ltrim=function()
{
return this.replace(/(^\s*)/g,"");
}
String.prototype.Rtrim=function()
{
return this.replace(/(\s*$)/g,"");
}
//EXAMPLE
var str=" hello ";
var show="head"+str.Rtrim()+"end";
alert(show);
{
return this.replace(/(^\s*)|(\s*$)/g,"");
}
String.prototype.Ltrim=function()
{
return this.replace(/(^\s*)/g,"");
}
String.prototype.Rtrim=function()
{
return this.replace(/(\s*$)/g,"");
}
//EXAMPLE
var str=" hello ";
var show="head"+str.Rtrim()+"end";
alert(show);
No.3 moveElement(),createElement()
function moveElement(){
var box=document.getElementById("box");
var p=document.getElementById("p");
box.appendChild(p);
}
function createElement(){
var box=document.getElementById("box");
var text=document.createElement("p");
text.innerHTML='选项框'+
'<select>'+
'<option value="1">选项A</option>'+
'<option value="1">选项B</option>'+
'</select>'+
'文本框'+
'<input type="text" />';
box.appendChild(text);
}
function activeEvent(){
var add=document.getElementById("add");
add.onclick=moveElement;
var create=document.getElementById("create");
create.onclick=createElement;
}
window.onload=activeEvent;
[html部分]var box=document.getElementById("box");
var p=document.getElementById("p");
box.appendChild(p);
}
function createElement(){
var box=document.getElementById("box");
var text=document.createElement("p");
text.innerHTML='选项框'+
'<select>'+
'<option value="1">选项A</option>'+
'<option value="1">选项B</option>'+
'</select>'+
'文本框'+
'<input type="text" />';
box.appendChild(text);
}
function activeEvent(){
var add=document.getElementById("add");
add.onclick=moveElement;
var create=document.getElementById("create");
create.onclick=createElement;
}
window.onload=activeEvent;
<div id="box">
选项框
<select>
<option value="1">选项A</option>
<option value="1">选项B</option>
</select>
文本框
<input type="text" />
</div>
<input id="add" type="button" value="添加" />
<input id="create" type="button" value="创建法" />
<!--下面的P元素在使用前首先定义为隐藏-->
<p id="p">选项框
<select>
<option value="1">选项A</option>
<option value="1">选项B</option>
</select>
文本框
<input type="text" /></p>
选项框
<select>
<option value="1">选项A</option>
<option value="1">选项B</option>
</select>
文本框
<input type="text" />
</div>
<input id="add" type="button" value="添加" />
<input id="create" type="button" value="创建法" />
<!--下面的P元素在使用前首先定义为隐藏-->
<p id="p">选项框
<select>
<option value="1">选项A</option>
<option value="1">选项B</option>
</select>
文本框
<input type="text" /></p>
No 4. for in 的使用:
var arr=employees;
for(var i in arr)
{
alert(arr[i].FirstName);
}
//在js中 for in 中的 变量(i) 就是对象的下标。
for(var i in arr)
{
alert(arr[i].FirstName);
}
//在js中 for in 中的 变量(i) 就是对象的下标。
NO.5 字符串转JSON
<script type="text/javascript">
<!--
var a=50,b="xxx";
var arr="{id:"+a+",name:'"+b+"'}";
arr=eval('('+arr+')')
alert(arr.name);
//-->
</script>
<!--
var a=50,b="xxx";
var arr="{id:"+a+",name:'"+b+"'}";
arr=eval('('+arr+')')
alert(arr.name);
//-->
</script>
■ 拿起勇气,探索你未知的世界。