设置没有值的属性 [英] Set attribute without value
本文介绍了设置没有值的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何设置数据属性而不在 jQuery 中添加值?我想要这个:
我试过了:
$('body').attr('data-body');//这是一个 getter,不起作用$('body').attr('data-body', null);//不添加任何东西
其他一切似乎都将第二个参数添加为字符串.是否可以只设置一个没有值的属性?
解决方案
attr()
函数也是一个 setter 函数.你可以只传递一个空字符串.
$('body').attr('data-body','');
空字符串只会创建没有值的属性.
<块引用>
参考 - http://api.jquery.com/attr/#attr-attributeName-value一个>
属性(属性名称,值)
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?
解决方案
The attr()
function is also a setter function. You can just pass it an empty string.
$('body').attr('data-body','');
An empty string will simply create the attribute with no value.
<body data-body>
Reference - http://api.jquery.com/attr/#attr-attributeName-value
attr( attributeName , value )
这篇关于设置没有值的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文