| [转帖]JavaScript中为空值判断
					当前位置:点晴教程→知识管理交流
					
					→『 技术文档交流 』
					
				 
  :Javascript中为空值判断  <html> <head> <meta charset="UTF-8"> <title>Javascript中为空判断</title> </head> <script type="text/javascript"> function stringDeelWith(){ var testValue=null; if(testValue=="" || testValue==undefined || undefined==null){ document.write(testValue); }else{ document.wirte("this is value not null or not undefined"); } } </script> <body> <input type="button" onclick="stringDeelWith()" value="button"> </body> </html> Javascript和Java不一样的地方是,Java中判断是否为空只要判断是否等于null就可以了,可是在Javascript中却不能这样,因为还存在在这另外的几种情况。 下面看看具体的例子: <html><head><meta charset="UTF-8"><title>Javascript中为空判断</title></head><script type="text/javascript">
	function stringDeelWith(){
		var testValue=null;
		if(testValue=="" || testValue==undefined || undefined==null){
			document.write(testValue);
		}else{
			document.wirte("this is value not null or not undefined");
		}
		
	}</script><body>
	<input type="button" onclick="stringDeelWith()" value="button"></body></html>因为在平常开发的时候一般会排除,变量里没有内容的空,变量没有定义的空,变量没有初始化的空。只要排除这三种情况就可以保证这个变量里是定义过的、是由内容的、是初始化过的,这个时候就可以放心的使用了 该文章在 2023/5/31 19:36:18 编辑过 | 关键字查询 相关文章 正在查询... |