简体   繁体   English

javascript检查对象是否实现HTMLAnchorElement接口

[英]javascript check if object implements HTMLAnchorElement interface

Is there a javascript way of determining if an object implements the 'HTMLAnchorElement' interface? 有没有一种JavaScript方法可以确定对象是否实现了“ HTMLAnchorElement”接口? When I do typeOf(someVariable) it returns 'object' . 当我做typeOf(someVariable)它返回'object' Can I take that a step further and verify the type implements the 'HTMLAnchorElement' interface? 我可以采取进一步措施,并验证该类型是否实现了“ HTMLAnchorElement”接口吗? Seems like it would be simple enough but I can't find any examples. 似乎很简单,但我找不到任何示例。 Any help or sample code would be appreciated. 任何帮助或示例代码将不胜感激。 Thanks! 谢谢!

You can use the instanceof operator. 您可以使用instanceof运算符。

Example: 例:

var a = document.links[0];
alert(a instanceof HTMLAnchorElement); // true if there's a link in the document

Note that IE7- doesn't define the HTMLAnchorElement object. 请注意,IE7-并未定义HTMLAnchorElement对象。 As a fallback, you can check for the tagName or nodeName property of a supposed element. 作为后备,您可以检查假定元素的tagNamenodeName属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM