无值设置属性 [英] Set attribute without value
问题描述
我设置了数据属性不增加jQuery中的值怎么办?我想这样的:
<车身数据身体GT;
我试过:
$('身体')ATTR('数据体')。 //这是一个getter,不工作
$(身体)ATTR('数据体',NULL); //不添加任何内容
一切似乎第二个参数添加为一个字符串。是否有可能只是设置一个属性没有价值?
的 ATTR()
功能也是setter函数。你可以把它传递一个空字符串。
$('身体')ATTR('数据体','');
这是空字符串只会创建没有值的属性。
<车身数据身体GT;
参考 - <一个href=\"http://api.jquery.com/attr/#attr-attributeName-value\">http://api.jquery.com/attr/#attr-attributeName-value
ATTR(的attributeName,值)
块引用>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屋!