js——undefined null 和forms[ ]用法

版权声明:转发博客 请注明出处 否则必究 https://blog.csdn.net/lucky541788/article/details/82078495

undefined和null
undefined:变量、属性和方法
null:对象

对象只有被定义才有可能为null,否则为undefined

//测试p是否存在
if(p !== null && typeof p !== 'undefined')//错误
if(typeof p !== 'undefined' && p !== null)//正确 先用typeof检测对象是否已定义

forms[ ]
forms[ ]:集合返回当前页面所有表单的数组集合
用法:

document.forms[].property
document.forms['form_name']['input_name']
<script>
console.log(document.forms['cc0'].innerHTML); //<input type="text">
console.log(document.forms['cc']['cc1']); //<input type="text" value="cc1" name="cc1">
</script>


<body>
<form action="" name="cc0">
    <input type="text">
</form>

<form action="" name="cc">
    <input type="text" value="cc1" name="cc1">
    <input type="text" value="cc2" name="cc2">
</form>
</body>

猜你喜欢

转载自blog.csdn.net/lucky541788/article/details/82078495