设置没有值的属性
如何在 jQuery 中设置数据属性而不添加值?我想要这个:
<body data-body>
我尝试过:
$('body').attr('data-body'); // this is a getter, not working
$('body').attr('data-body', null); // not adding anything
其他一切似乎都将第二个参数添加为字符串。是否可以只设置一个没有值的属性?
How do I set a data attribute without adding a value in jQuery? I want this:
<body data-body>
I tried:
$('body').attr('data-body'); // this is a getter, not working
$('body').attr('data-body', null); // not adding anything
Everything else seems to add the second arguments as a string. Is it possible to just set an attribute without value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
attr()
函数也是一个 setter 函数。您可以只向它传递一个空字符串。空字符串将简单地创建没有值的属性。
The
attr()
function is also a setter function. You can just pass it an empty string.An empty string will simply create the attribute with no value.
也许尝试:
Perhaps try:
接受的答案不再创建仅名称属性(截至 2017 年 9 月)。
您应该使用 JQuery prop() 方法来创建仅名称属性。
The accepted answer doesn't create a name-only attribute anymore (as of September 2017).
You should use JQuery prop() method to create name-only attributes.
不用 jQuery 也能做到!
例子:
You can do it without jQuery!
Example:
不确定这是否真的有益,或者为什么我更喜欢这种风格,但我所做的(在 vanilla js 中)是:
这可能会以全小写形式添加没有值的属性,或者如果元素上已存在该属性,则将其删除。
来源: toggleAttribute
Not sure if this is really beneficial or why I prefer this style but what I do (in vanilla js) is:
This may add the attribute in all lowercase without a value or remove it if it already exists on the element.
Source: toggleAttribute
只需尝试一下,它肯定会工作......
document.querySelector("audio").setAttribute("autoplay", "");
这将显示如下代码;-
如果你写的是,
$("audio").attr("autoplay", "");
然后,这将显示如下代码;-
simply try this, it will definately work....
document.querySelector("audio").setAttribute("autoplay", "");
this will showed like below code;-
if you wrote like,
$("audio").attr("autoplay", "");
then, this will showed like below code;-
我们这里有很多好的答案。
但是,您必须看到在 Firefox 内部它为您提供
data-body=""
,而在 Chrome 内部它为您提供data-仅正文。
We have a lot of good answers here.
But, you have to see that inside Firefox it gives you
data-body=""
and inside Chrome it gives youdata-body
only.这在 HTML 中是不可能的。
属性必须有一个值
根据 HTML 标准:
为什么我看到属性没有值?
您在 HTML 代码中看到没有值的属性的原因是 HTML 标准规定:
因此,
只是
的缩写形式。
那我该怎么办呢?
(如果您能够使用现代 JS,您应该简单地调用
document.body.setAttribute("data-body", "")
。)您的浏览器元素检查器可以自由地将属性显示为 <代码>data-body (Chrome) 或
data-body=""
(Firefox)。(缩短并改编自 https://8hob.io/posts/在 js 中设置无值属性/)
This is impossible in HTML.
Attributes must have a value
According to the HTML standard:
Why do I see attributes with no value?
The reason you see an attribute without a value in HTML code is because the HTML standard states:
Therefore,
<body data-body>
is simply a shortened form of<body data-body="">
.So what should I do?
(If you are able to use modern JS, you should simply call
document.body.setAttribute("data-body", "")
.)Your browser element inspector is free to display the attribute as
data-body
(Chrome) ordata-body=""
(Firefox).(Shortened and adapted from https://8hob.io/posts/set-attribute-without-value-in-js/)