<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>检查指定的值是不是空字符串</title><script>function test_1() {// var name = document.getElementById("name").value;// alert(name);var name = null;// var flag = isEmptyStr(name);var flag = isEmptyStrV2(name);alert(flag);}function test_2() {var flag_1 = null == undefined; // truevar flag_2 = null === undefined; // falsealert(flag_1);alert(flag_2);}function test_3(s) {alert(s);var flag = isEmptyStr(s);alert(flag);}function isEmptyStr(s) {if (s == undefined || s == null || s.trim() === '') {return true}return false}function isEmptyStrV2(s) {if (typeof s == 'string' && s.trim().length == 0) {return true;} else if (s == null) {return true;}return false;}// 因为程序将null等同于undefined,所以可以将上述函数中if语句中的条件语句简化成下面函数所示的条件语句function isEmptyStr(s) {if (s == undefined || s.trim() === '') {return true}return false}</script>
</head>
<body>
<label for="name">name:</label>
<input id="name" type="text" name="name"/>
<button type="button" onclick="var name = document.getElementById('name').value;test_3(name);">Click Me!</button>
<!--<button type="button" onclick="test_1();">Click Me!</button>-->
<!--<button type="button" onclick="test_2();">Click Me!</button>-->
</body>
</html>
参考:https://blog.csdn.net/K346K346/article/details/113182838