简体   繁体   中英

using jQuery, how can I remove a dynamically added class to the head of a page

I use an inline editor to edit css classes, when a change is made I wish to remove the class definition and add it again, also user has option to delete the element using it so I need to delete the definition.

Adding works using this code:

$("<style>").prop("type", "text/css").html( "#my_element_"+MaxElements+" {"+ xCSSCode +"}").appendTo("head");

however I can't seem to remove this class which is inserted into the head of the page as follows:

<style type="text/css">#my_element_1 {border-radius: 12.5px;
...
}</style>

create a style tag:

var style = $("<style />", {
                id  : 'myStyleTag',
                type: 'text/css',
                html: "#my_element_"+MaxElements+" {"+ xCSSCode +"}"
}).appendTo("head");

to remove

style.remove();
// or
$('#myStyleTag').remove();

I would store the elements in an object:

var styles = {};

...

styles[some_identifier] = $("<style>", {
    type: "text/css",
    html: "#my_element_"+MaxElements+" {"+ xCSSCode +"}"
}).appendTo("head");

You can remove the style tag easily:

styles[some_identifier].remove();
 $('#my_element').removeClass('hidden');
 // also
 $('#my_element').addClass('hidden');

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