Is there any way to check if some HTML attribute are Boolean? for example:
有没有办法检查某些HTML属性是否为布尔值?例如:
<input type="text" name="input" disabled=""/>
Here the disabled
attribute is Boolean, I have some code and I need check before setting value whether that attribute is Boolean or not.
这里的disabled属性是布尔值,我有一些代码,我需要在设置值之前检查该属性是否为布尔值。
Why I need this?
为什么我需要这个?
As mentioned here we can have either ""
or property name itself as the valid value not true
or false
.
如上所述,我们可以将“”或属性名称本身作为有效值,而不是true或false。
2 个解决方案
#1
2
There's basically no distinction on the level of HTML. If the attribute is simply the name without value, e.g. <input disabled>
, that's a sure sign that it's a boolean attribute. However, if it's using the name="value"
notation, then there's no way to distinguish it. Is class="class"
a boolean attribute? No, it's a classList
with one entry "class"
. How about foo=""
? Well, it's either a boolean attribute opting for the empty-value notation, or it's an attribute with no value set.
HTML的级别基本上没有区别。如果该属性只是没有值的名称,例如,这是一个肯定的标志,它是一个布尔属性。但是,如果它使用name =“value”表示法,那么就无法区分它。 class =“class”是一个布尔属性吗?不,它是一个带有一个条目“class”的classList。 foo =“”怎么样?好吧,它是选择空值表示法的布尔属性,或者是没有设置值的属性。
Only the interpreter assigns boolean-ness to an attribute; i.e. while parsing the HTML into a DOM, the interpreter sets DOM attributes like this, roughly speaking:
只有解释器为属性分配boolean-ness;即,在将HTML解析为DOM时,解释器会像这样设置DOM属性,粗略地说:
domElement.disabled = htmlElement.hasAttribute('disabled');
If you want to know what HTML elements are booleans, you need to do the same thing an interpreter does: keep a list of DOM elements whose attributes have types and interpret the HTML according to that specification.
如果你想知道哪些HTML元素是布尔值,你需要做一个解释器做的事情:保留一个DOM元素列表,其元素的属性有类型,并根据该规范解释HTML。
#2
0
To solve this issue, you have the typeof
operand in the following way:
要解决此问题,请按以下方式使用typeof操作数:
var check_input = document.getElementById("check-input");
if(typeof(check_input.disabled) === "boolean"){
alert('Yes');
}
Here is a JSfiddle with the complete code. I hope that my answer can help you!
这是一个带有完整代码的JSfiddle。我希望我的回答可以帮到你!
#1
2
There's basically no distinction on the level of HTML. If the attribute is simply the name without value, e.g. <input disabled>
, that's a sure sign that it's a boolean attribute. However, if it's using the name="value"
notation, then there's no way to distinguish it. Is class="class"
a boolean attribute? No, it's a classList
with one entry "class"
. How about foo=""
? Well, it's either a boolean attribute opting for the empty-value notation, or it's an attribute with no value set.
HTML的级别基本上没有区别。如果该属性只是没有值的名称,例如,这是一个肯定的标志,它是一个布尔属性。但是,如果它使用name =“value”表示法,那么就无法区分它。 class =“class”是一个布尔属性吗?不,它是一个带有一个条目“class”的classList。 foo =“”怎么样?好吧,它是选择空值表示法的布尔属性,或者是没有设置值的属性。
Only the interpreter assigns boolean-ness to an attribute; i.e. while parsing the HTML into a DOM, the interpreter sets DOM attributes like this, roughly speaking:
只有解释器为属性分配boolean-ness;即,在将HTML解析为DOM时,解释器会像这样设置DOM属性,粗略地说:
domElement.disabled = htmlElement.hasAttribute('disabled');
If you want to know what HTML elements are booleans, you need to do the same thing an interpreter does: keep a list of DOM elements whose attributes have types and interpret the HTML according to that specification.
如果你想知道哪些HTML元素是布尔值,你需要做一个解释器做的事情:保留一个DOM元素列表,其元素的属性有类型,并根据该规范解释HTML。
#2
0
To solve this issue, you have the typeof
operand in the following way:
要解决此问题,请按以下方式使用typeof操作数:
var check_input = document.getElementById("check-input");
if(typeof(check_input.disabled) === "boolean"){
alert('Yes');
}
Here is a JSfiddle with the complete code. I hope that my answer can help you!
这是一个带有完整代码的JSfiddle。我希望我的回答可以帮到你!