日期:2014-05-16  浏览次数:20387 次

javascript基础知识小结
javascript基础知识

undefind:表示不确定的类型,只是定义了一个变量,但具体什么类型并不知道,也就是var j; 只定义但并没有赋值

Javascript类型转换
转换为Number 通过parseInt进行转换
转换为String 任何数据类型+String类型=String 类型
转换为Boolean ture false 所有非0数字为true,反之为false.
Undefined,null转换为false

如:
if(undefined||null){
	alert("haha");
}else{
	//会走这里
	alert("nohaha");
}
if(2){//会走这里
	alert("haha");
}else{
	alert("nohaha");
}


数据传递:
基本类型为:值传递
  function addNum(i){
    	i = i + 5;
  }
  function test(){
	var i = 0;
	addNum(i);
	alert(i);//打印出来的值为0
  }


函数和事件通常是一种绑定关系,通过事件调用函数。如果绑定多个函数,中间用分号隔开

<marquee onmouseover="this.stop()" onmouseout="this.start()">跑马灯效果,欢迎大家学习!</marquee>




选择之后直接新打开一个页面:
function goToUrl(){
	var s = document.getElementById("toUrl");
	if(s.options[s.selectedIndex].value != -1){
		//window.location.href="http://www." + s.options[s.selectedIndex].value + ".com";
		window.open("http://www." + s.options[s.selectedIndex].value + ".com","_blank");
	}
}

<select onchange="goToUrl();" id="toUrl">
	<option value="-1">请选择要去的网站</option>
	<option value="sina">新浪网</option>
	<option value="baidu">百度</option>
</select>


***************************************************
这段代码是屏闭鼠标右键功能及复制功能
<body onmouseup="document.selection.empty();" oncontextmenu="return false;" 
  onmousemove="document.selection.empty(); onCopy="document.selection.empty();"
   onselect="document.selection.empty();">
################################################################
动态添加事件:
<script type="text/javascript">
		function mt1(){
			alert("1");
		}
		function mt2(){
			alert("2");
		}
		function mt3(){
			alert("3");
		}
		function init(){
			var btn1 = document.getElementById("button1");
			if(window.ActiveXObject){
				//这是IE浏览器,需要写全 onclick,也不需要false
				btn1.attachEvent("onclick",mt1);
			}else{//这是firefox浏览器
				btn1.addEventListener("click",mt1,false);
			}
			
			//btn1.addEventListener("click",mt2,false);
			//btn1.addEventListener("click",mt3,false);
		}
	</script>
  </head>
  
  <body onload="init();">
  	<input type="button" value="button1" id="button1"/>
  </body>

########################################################################
全选
隐藏,显示层
折叠菜单效果
###################################################################
<script language="javascript">
	function selectAll(){
		var al = document.getElementById("all");
		/*全选功能
		if(al.checked == true){
			var cbs = document.getElementsByName("cb");
			for(var i = 0; i < cbs.length; i++){
				cbs[i].checked = true;
			}
		}else{
			var cbs = document.getElementsByName("cb");
			for(var i = 0; i < cbs.length; i++){
				cbs[i].checked = false;
			}
		}
		*/	
		//全选功能
		var cbs = document.getElementsByName("cb");
		for(var i = 0; i < cbs.length; i++){
			cbs[i].checked = al.checked;
		}	
	}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p>
    全选
      <input type="checkbox" name="checkbox2" value="checkbox" id="all" onclick="selectAll();"/>
  </p>
  <p>
    <input type="checkbox" name="cb" value="checkbox" />
    <input type="checkbox" name="cb" value="checkbox" />  
    <input type="checkbox" name="cb" value="checkbox" />  
    <input type="checkbox" name="cb" value="checkbox" />  
    <input type="checkbox" name="cb" value="checkbox" />  
    <input type="checkbox" name="cb" value="checkbox" />  
    <input type="checkbox" name="cb" value="checkbox" />  
    <input type="check