js使用总结

1.周期性运行函数

setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。
举例:

<input type="button" value="開始计时!" onClick="timedCount()">
<script type="text/javascript">
	var c=0;
	var t;
	function timedCount()
	{
		document.getElementById('txt').value=c;
		c=c+1;
		t=setTimeout("timedCount()",1000);
	}
</script>
或者:
<input type="button" value="開始计时!" onClick="timedCount(0)">
<script type="text/javascript">
	function timedCount(c)
	{
		document.getElementById('txt').value=c;
		c=c+1;
		var x=function(){
			timedCount(c);
		}
		setTimeout(x,1000);
	}	
</script>
2.js中怎样推断对象是否存在

typeof函数:
typeof 能够用来检測给定变量的数据类型。可能的返回值:
1. 'undefined' --- 这个值没有定义;
2. 'boolean'    --- 这个值是布尔值;
3. 'string'        --- 这个值是字符串;
4. 'number'     --- 这个值是数值;
5. 'object'       --- 这个值是对象或null;
6. 'function'    --- 这个值是函数。


举例:
if(typeof(key)!="undefined"&&key != ''){ //do something}

3.js中替换空格

使用a.replace(/\s+/g,'');举例:

<script type="text/javascript">
	var a = ' 11 222 33 44 55 ';
	a = a.replace(/\s+/g,'');
	alert(a);
</script>
4.字符串切割函数

split() :把一个字符串切割成字符串数组。


stringObject.split(separator,howmany)
举例:

<script type="text/javascript">
	var data = "1,2,3,4";
	var arr = data.split(",");
	alert(arr[0]);
</script>
5.删除数组中的元素

splice():删除数组中的元素
arrayObject.splice(index,howmany,item1,.....,itemX)
index:必需。整数,规定加入/删除项目的位置,使用负数可从数组结尾处规定位置。
howmany: 必需。要删除的项目数量。假设设置为 0,则不会删除项目。

 
item1, ..., itemX: 可选。向数组加入的新项目。 
说明:
splice() 方法可删除从 index 处開始的零个或多个元素。而且用參数列表中声明的一个或多个值来替换那些被删除的元素。假设从 arrayObject 中删除了元素,则返回的是含有被删除的元素的数组。

6.JSON转换为字符串
str = JSON.stringify(data); //data是json类型的数据

7.查找子字符串个数
//在data中查找song_id的个数
var reg=new RegExp('song_id',"gi");
count = str.match(reg).length;

8.监听输入框变化

function immediately(){
	var element = document.getElementById("title");
	if("\v"=="v") {
		element.onpropertychange = webChange;
	}else{
		element.addEventListener("input",webChange,false);
	}
	function webChange(){
		if(element.value){
			//do something with element.value						
		};
	}
}
immediately();
參考:http://www.jb51.net/article/27684.htm

9.删除div下全部子节点

 function removeAllChild(){
    var div = document.getElementById("songLink");
    while(div.hasChildNodes()){ //当div下还存在子节点时 循环继续
        div.removeChild(div.firstChild);
    }
}
10.jquery改动操作css属性
在jquery中使用css()方法便能够css属性实现动态改动,以下介绍经常用法:
1.获取css属性:$(selector).css(name)
取得第一个段落的 color 样式属性的值:$("p").css("color");
2.设置css属性:$(selector).css(name,value)
将全部段落的颜色设为红色:$("p").css("color","red");
3.使用函数来设置CSS属性:$(selector).css(name,function(index,value))
此函数返回要设置的属性值。接受两个參数,index 为元素在对象集合中的索引位置(可选),value 是原先的属性值(可选)。


将全部段落的颜色设为红色:
$("button").click(function(){
    $("p").css("color",function(){return "red";});
});
4.设置多个CSS属性/值对:$(selector).css({property:value, property:value, ...})
$("p").css({
  "color":"white",
  "background-color":"#98bf21",
  "font-family":"Arial",
  "font-size":"20px",
  "padding":"5px"
  });
 注:jquery能够使用attr()函数设置属性值,使用方法同css()方法
 详情參考Jquery属性操作


本文为Eliot原创,转载请注明出处:http://blog.csdn.net/xyw_blog/article/details/40432313

posted @ 2017-06-30 16:36  brucemengbm  阅读(200)  评论(0编辑  收藏  举报