jquery 设置数据属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12872176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:04:52  来源:igfitidea点击:

jquery set data attr

jquery

提问by yasaricli

Html

html

<div id="you" data-you="Hello mean">super</div>

is #you html element change data-you attribute

是 #you html 元素更改 data-you 属性

console.log($("#you").data("you")); // Hello mean

$("#you").attr("data-you", "yes change you atribute");

console.log($("#you").data("you")); // Hello mean | does not change.

Attribute "data-you" does not change when I change. How can I do this?

当我改变时,属性“data-you”不会改变。我怎样才能做到这一点?

thank you.

谢谢你。

回答by Raj Nathani

There has already been an answer chosen as a correct one, however its seems like none of the answers clearly and concisely explain what is happening.
So let me give this a shot:

已经有一个答案被选为正确的答案,但是似乎没有一个答案清楚而简洁地解释了正在发生的事情。
所以让我试一试:

$(element).data(key, value)does notchange the html5 'data-*' attributes of the element, jQuery internally stores the key-value (in jQuery.cache).
As a result when you call $(element).data(key)you get what is stored internally by jQuery.

$(element).data(key, value)不会更改元素的 html5 'data-*' 属性,jQuery 在内部存储键值(在 jQuery.cache 中)。
因此,当您调用时,您将$(element).data(key)获得 jQuery 内部存储的内容。

To answer your question here:

在这里回答你的问题:

Since you are looking to change the data-youattribute of your html tag you will instead need to use the attr()method

由于您希望更改data-youhtml 标签的属性,因此您需要使用该attr()方法

Thus:

因此:

console.log($("#you").attr("data-you")); // Hello mean

$("#you").attr("data-you", "yes change you atribute");

console.log($("#you").attr("data-you")); // The data-you attribute has been changed.

回答by yasaricli

attr() And you can not have to change the data() method.

attr() 并且您不必更改 data() 方法。

Try the following way:

尝试以下方式:

console.log($("#you").data("you")); // Hello mean

$("#you").data("you", "yes change you atribute"); // yes change you atribute

console.log($("#you").data("you")); //  yes change you atribute

examples of data http://api.jquery.com/data/??????

数据示例 http://api.jquery.com/data/??????

回答by Siva Charan

On your same logic, to see the result; change datato attr

按照你的逻辑,看看结果;更改dataattr

console.log($("#you").attr("data-you"));

Refer LIVE DEMO

参考现场演示

回答by Liam

Try this:

尝试这个:

$("#you").data("you", "yes change you atribute");

回答by Developer

For More Info ==> How to Get & Set Data Attribute Value From HTML Elements jQuery - Tutsmake

更多信息 ==>如何从 HTML 元素 jQuery 获取和设置数据属性值 - Tutsmake

Using the below syntax you can set an attribute and values.

使用以下语法,您可以设置属性和值。

$(selector).attr(attribute,value);  

For Set data Attribute value By Example

For Set data Attribute value By Example

$('#myClass').attr("data-id",50); 

回答by ANCY MATHEW

$("#you").attr("data-you","New Value");