简体   繁体   English

当data- *属性更改时如何更新元素

[英]How to update element when data-* attribute changed

I have an element with following css style: #element:after { content: attr(data-percent); } 我有一个具有以下CSS样式的元素: #element:after { content: attr(data-percent); } #element:after { content: attr(data-percent); }

I'm trying to change data-percent attribute with JQuery like this $('#element').data('percent', '50%'); 我正在尝试使用$('#element').data('percent', '50%'); #element $('#element').data('percent', '50%');这样的JQuery更改data-percent属性$('#element').data('percent', '50%');

Attribute changes, I can check it by console.log($('#element').data('percent')); 属性更改,我可以通过console.log($('#element').data('percent')); , but it won't redraw and still contains old attribute. ,但它不会重绘并且仍然包含旧属性。

What function should I call to make this element updated? 我应该调用什么函数来更新此元素?

jQuery's data takes the attribute as the initial value , but it doesn't use it to actually store the data, which is cached in memory instead. jQuery的data该属性作为初始值 ,但它并不使用它实际存储数据,而是将其缓存在内存中。

This allows to store whatever value you want, instead of mere strings as per HTML5 standards. 这样就可以存储所需的任何值,而不是按照HTML5标准存储字符串。

If you actually want to use the attribute to store the data, use 如果您确实想使用该属性来存储数据,请使用

$('#element').attr('data-percent', '50%');

jQuery cannot reference and/or end :after or :before pseudo elements. jQuery不能引用和/或以伪元素结束:after或:before。 CSS elements do not appear to automatically update what the referenced attr() changes. CSS元素似乎不会自动更新引用的attr()的更改。

My suggestion would be to not use a pseudo element. 我的建议是不要使用伪元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM