简体   繁体   中英

How to remove/change dynamically created CSS class in JavaScript

Background ,

There are some elements where CSS animations needs to be applied. However these CSS needs to be generated on the fly as calculation needs to be done from JavaScript code.

This question clarifies how to dynamically add a CSS. Now the question is, how do I remove or replace a CSS generated following the accepted answer?

Just to be clear, it's nothing to do with removing a class from element. I need to remove it from the page so that animations will stop from elements with class. At some time later I can alter the CSS and re-register class so that associated elements are animated in different way.

Edit.

You can remove it from the element's class list

document.getElementById("myelementsid").classList.remove("classname");

这里是使用jQuery https://api.jquery.com/removeclass/的完整文档,可以删除一个或多个类

document.getElementById('someElementId')。className ='';

If you want to remove the specific style from the page you can do it like the following snippet:

$('link[title=stylesheettitle]').prop('disabled',true);

OR

$('link[title="stylesheettitle"]').remove();

Java Script

var style = document.getElementById('styleID');
style.parentNode.removeChild(style );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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