admin管理员组文章数量:1335624
I have an array of elements:
var elements = ['div', 'a', 'p', 'foo']
I also have an array of attributes:
var attributes = ['src', 'href', 'quux', 'id']
I want to understand how I can validate if the binations of the above would make a valid DOM object.
Or, in other words:
How can I validate a DOM object against the DOM schema?*
For example (based on the above elements and attributes):
DOM_result = '<div src />'; // = false
DOM_result = '<div href />'; // = false
DOM_result = '<div quux />'; // = false
DOM_result = '<div id />'; // = true
DOM_result = '<a src />'; // = false
DOM_result = '<a href />'; // = true
DOM_result = '<a quux />'; // = false
DOM_result = '<a id />'; // = true
etc...
* = not sure if this is called DOM schema.
PS:
In my example I'm using JS, but please consider this question scripting language agnostic.
I have an array of elements:
var elements = ['div', 'a', 'p', 'foo']
I also have an array of attributes:
var attributes = ['src', 'href', 'quux', 'id']
I want to understand how I can validate if the binations of the above would make a valid DOM object.
Or, in other words:
How can I validate a DOM object against the DOM schema?*
For example (based on the above elements and attributes):
DOM_result = '<div src />'; // = false
DOM_result = '<div href />'; // = false
DOM_result = '<div quux />'; // = false
DOM_result = '<div id />'; // = true
DOM_result = '<a src />'; // = false
DOM_result = '<a href />'; // = true
DOM_result = '<a quux />'; // = false
DOM_result = '<a id />'; // = true
etc...
* = not sure if this is called DOM schema.
PS:
In my example I'm using JS, but please consider this question scripting language agnostic.
1 Answer
Reset to default 9Most content attributes are reflected by an IDL attribute (a.k.a property) with the same name.
Those IDL attributes are implemented as accessor properties (i.e. getters and setters) in an interface from which the elements inherit from.
Therefore, you can create an element of the desired type and check if it has the desired property:
'src' in document.createElement('div'); // false
'src' in document.createElement('img'); // true
Note IDL attributes are case-sensitive, and some have different names than content attributes, e.g. you should check className
instead of class
.
本文标签: javascriptHow to check if DOM element andor attribute is validStack Overflow
版权声明:本文标题:javascript - How to check if DOM element andor attribute is valid? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742304444a2449631.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论