小编Joe*_*uño的帖子

为什么不能在instanceof HTMLInputElement上使用“ hasOwnProperty”?

我想检查输入元素是复选框还是文本类型。

我知道我可以这样做:

//Type of input..
if ( input.type === "checkbox" ) 
//Contains the property..
if ( "checked" in input ) 
Run Code Online (Sandbox Code Playgroud)

但是我的问题是:为什么hasOwnProperty返回false?

我只想使用:

input.hasOwnProperty("checked")
Run Code Online (Sandbox Code Playgroud)

但每次都会返回false。

不是input物体吗?
我不这么认为,但是typeof说是:

typeof input // returns "object" 
Run Code Online (Sandbox Code Playgroud)

那么发生了什么?!

代码示例:

//Type of input..
if ( input.type === "checkbox" ) 
//Contains the property..
if ( "checked" in input ) 
Run Code Online (Sandbox Code Playgroud)
input.hasOwnProperty("checked")
Run Code Online (Sandbox Code Playgroud)

有关HTMLInputElement的文档,只有type复选框具有以下属性checked

javascript hasownproperty

6
推荐指数
1
解决办法
80
查看次数

标签 统计

hasownproperty ×1

javascript ×1