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

javascript中,null与""有什么区别,我感觉一样啊
<html>
<head>
<title>123</title>
<script>
function validate(){
var text1 = document.getElementById('text');
if(text1==null){
alert("1");
}else{
alert("2");
}
}
</script>

</head>
<body>

<form>
  <input type="text" id="text">
<input type="button" value="提交" onclick="validate();">
</form>
</body>
</html>
为啥text1不为空啊

------解决方案--------------------
public class test01 {
public static void main(String[] args) {

String str1 = "";
//str1在内存开辟了空间,只是没加内容,长度输出0
System.out.println(str1.length());
String str2 = null;
//str1没有在内存开辟空间,输出空指针异常
System.out.println(str2.length());
}
}