如何使用 JavaScript 添加布尔属性

发布于 2025-01-03 23:28:03 字数 186 浏览 3 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

榆西 2025-01-10 23:28:03

添加布尔属性:

node.setAttribute(attributeName, '');
// example:
document.body.setAttribute('hidden', '');

注意第二个参数为空字符串

使用 node.removeAttribute(attributeName) 删除其他人提到的属性。

To add a Boolean attribute:

node.setAttribute(attributeName, '');
// example:
document.body.setAttribute('hidden', '');

Note the empty string as the second argument!

Use node.removeAttribute(attributeName) to remove an attribute as mentioned by others.

情绪 2025-01-10 23:28:03

一般来说,您可以使用 element.setAttribute('attributeName', 'value')element.propertyName = value 来切换元素的属性或特性。

布尔属性

对于布尔属性,请设置具有相同名称值的属性:

element.setAttribute('disabled', 'disabled');

删除布尔属性的工作方式与其他属性相同:

element.removeAttribute('disabled');

但是,您的两个示例都不是 布尔属性

contenteditable

contenteditable 不是布尔属性,而是枚举属性。其可能的值为空字符串、"true""false"

虽然在这种情况下 setAttribute 似乎有些过分,但您可以使用它:

element.setAttribute('contenteditable', 'true');
// to undo:
element.removeAttribute('contenteditable');

contenteditable 属性的属性名称是 contentEditable (注意大写的 E),并且它识别值 'true''false''inherit' — 因此您可以使用:

element.contentEditable = 'true';
// to undo:
element.contentEditable = 'false';

请注意 'true' 和 'false' 这里是字符串,而不是布尔值。

data-example

对于 data-example 属性,您可以使用:

element.setAttribute('data-example', 'some value'); // the value should be a string
// to undo:
element.removeAttribute('data-example');

或者,在支持 dataset 的浏览器中(请参阅以浅绿色突出显示的内容)在 http://caniuse.com/dataset 上,您可以使用:

element.dataset.example = 'some value';

In general, you can use element.setAttribute('attributeName', 'value') or element.propertyName = value to toggle an element’s attributes or properties.

Boolean attributes

For boolean attributes, set the attribute with the same-named value:

element.setAttribute('disabled', 'disabled');

Removing a boolean attribute works the same way as other attributes:

element.removeAttribute('disabled');

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:

element.setAttribute('contenteditable', 'true');
// to undo:
element.removeAttribute('contenteditable');

The property name for the contenteditable attribute is contentEditable (note the capital E), and it recognizes the values 'true', 'false', and 'inherit' — so you could just use:

element.contentEditable = 'true';
// to undo:
element.contentEditable = 'false';

Note that 'true' and 'false' are strings here, not booleans.

data-example

For the data-example attribute, you could use:

element.setAttribute('data-example', 'some value'); // the value should be a string
// to undo:
element.removeAttribute('data-example');

Or, in browsers who support dataset (see the ones highlighted in light green on http://caniuse.com/dataset), you could use:

element.dataset.example = 'some value';
不交电费瞎发啥光 2025-01-10 23:28:03

设置属性

使用 element.setAttributehttps://developer .mozilla.org/en/DOM/element.setAttribute

如果你像这样添加一个id

<p id="p1">

你可以像这样选择元素:

var p1 = document.getElementById("p1"); 

设置一个布尔属性

根据W3C HTML4 规范

布尔属性可以合法地采用单个值:属性本身的名称

因此您可以像这样添加属性:

p1.setAttribute("contenteditable", "contenteditable");

设置 contentEditable

根据 W3C HTML5 规范,属性 < code>contentEditable 可以有值 truefalseinherit。那么你就必须做这样的事情:

p1.setAttribute("contenteditable", "true");

说实话,我也不确定哪一个最适合你的情况。

To set an attribute

Use element.setAttribute: https://developer.mozilla.org/en/DOM/element.setAttribute

If you add an id like this:

<p id="p1">

you can select the element like this:

var p1 = document.getElementById("p1"); 

To set a Boolean attribute

According to the W3C HTML4 specification:

Boolean attributes may legally take a single value: the name of the attribute itself

so you can add your attribute like this:

p1.setAttribute("contenteditable", "contenteditable");

To set contentEditable

According to the W3C HTML5 specification, the attribute contentEditable can have values true, false and inherit. Then you would have to do something like this:

p1.setAttribute("contenteditable", "true");

To be honest, I am also not sure which one is best in your situation.

惯饮孤独 2025-01-10 23:28:03
element.setAttribute('contenteditable','contenteditable')

或清除:

element.removeAttribute('contenteditable')
element.setAttribute('contenteditable','contenteditable')

or to clear:

element.removeAttribute('contenteditable')
勿忘初心 2025-01-10 23:28:03

使用 element.dataset.example 修改 data-example 属性的值。

Use element.dataset.example to modify the value of the data-example attribute.

流云如水 2025-01-10 23:28:03

引自 MDN

如果布尔属性出现在
根本没有元素,无论其实际价值如何;一般来说,你
应该在值中指定空字符串(“”)(有些人使用
属性的名称;这可以工作,但不是标准的)。

来源: https://developer .mozilla.org/en-US/docs/Web/API/Element/setAttribute

Quoted from MDN:

Boolean attributes are considered to be true if they're present on the
element at all, regardless of their actual value; as a rule, you
should specify the empty string ("") in value (some people use the
attribute's name; this works but is non-standard).

source: https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文