开发者

Check if an variable is refer to designate object in JavaScript

开发者 https://www.devze.com 2023-04-02 06:19 出处:网络
Suppose I have an variable assign to 开发者_如何学编程an \"video\" tag, how can I check if this variable is refer to the object of \"HTMLVideoElement\"

Suppose I have an variable assign to 开发者_如何学编程an "video" tag, how can I check if this variable is refer to the object of "HTMLVideoElement"

var video = document.getElementById('video')
if (video != object HTMLUnknownElement) {
//some code here
}

What is the code to replace with object HTMLUnknownElement above?

Thanks


typeof is pretty useless, it'll just tell you that it's an object. You'd do better with instanceof: video instanceof HTMLVideoElement.

EDIT: Some browsers don't know what an HTMLVideoElement is, so to protect against them throwing an exception here you should check for it: typeof HTMLVideoElement !== "undefined" && video instanceof HTMLVideoElement.

0

精彩评论

暂无评论...
验证码 换一张
取 消