如何使用 JavaScript 添加布尔属性
如何使用 JavaScript 添加布尔属性?例如,如何将
更改为
更改为
How do you add boolean attributes using JavaScript? For example, how can you change:
<p>
to <p contenteditable>
<p>
to <p data-example>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
添加布尔属性:
使用
node.removeAttribute(attributeName)
删除其他人提到的属性。To add a Boolean attribute:
Use
node.removeAttribute(attributeName)
to remove an attribute as mentioned by others.一般来说,您可以使用
element.setAttribute('attributeName', 'value')
或element.propertyName = value
来切换元素的属性或特性。布尔属性
对于布尔属性,请设置具有相同名称值的属性:
删除布尔属性的工作方式与其他属性相同:
但是,您的两个示例都不是 布尔属性!
contenteditable
contenteditable
不是布尔属性,而是枚举属性。其可能的值为空字符串、"true"
和"false"
。虽然在这种情况下
setAttribute
似乎有些过分,但您可以使用它:contenteditable
属性的属性名称是contentEditable
(注意大写的E
),并且它识别值'true'
、'false'
和'inherit'
— 因此您可以使用:请注意
'true' 和
'false'
这里是字符串,而不是布尔值。data-example
对于
data-example
属性,您可以使用:或者,在支持
dataset
的浏览器中(请参阅以浅绿色突出显示的内容)在 http://caniuse.com/dataset 上,您可以使用:In general, you can use
element.setAttribute('attributeName', 'value')
orelement.propertyName = value
to toggle an element’s attributes or properties.Boolean attributes
For boolean attributes, set the attribute with the same-named value:
Removing a boolean attribute works the same way as other attributes:
However, neither of your two examples are boolean attributes!
contenteditable
contenteditable
is not a boolean attribute, it’s an enumerated attribute. Its possible values are the empty string,"true"
, and"false"
.While
setAttribute
seems overkill in this case, you could use it:The property name for the
contenteditable
attribute iscontentEditable
(note the capitalE
), and it recognizes the values'true'
,'false'
, and'inherit'
— so you could just use:Note that
'true'
and'false'
are strings here, not booleans.data-example
For the
data-example
attribute, you could use:Or, in browsers who support
dataset
(see the ones highlighted in light green on http://caniuse.com/dataset), you could use:设置属性
使用
element.setAttribute
:https://developer .mozilla.org/en/DOM/element.setAttribute如果你像这样添加一个
id
:你可以像这样选择元素:
设置一个布尔属性
根据W3C HTML4 规范:
因此您可以像这样添加属性:设置 contentEditable
根据 W3C HTML5 规范,属性 < code>contentEditable 可以有值
true
、false
和inherit
。那么你就必须做这样的事情:说实话,我也不确定哪一个最适合你的情况。
To set an attribute
Use
element.setAttribute
: https://developer.mozilla.org/en/DOM/element.setAttributeIf you add an
id
like this:you can select the element like this:
To set a Boolean attribute
According to the W3C HTML4 specification:
so you can add your attribute like this:To set contentEditable
According to the W3C HTML5 specification, the attribute
contentEditable
can have valuestrue
,false
andinherit
. Then you would have to do something like this:To be honest, I am also not sure which one is best in your situation.
或清除:
or to clear:
使用
element.dataset.example
修改data-example
属性的值。Use
element.dataset.example
to modify the value of thedata-example
attribute.引自 MDN:
来源: https://developer .mozilla.org/en-US/docs/Web/API/Element/setAttribute
Quoted from MDN:
source: https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute