6

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>
4

3 回答 3

10

创建样式标签:

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

去除

style.remove();
// or
$('#myStyleTag').remove();
于 2013-07-23T22:21:20.220 回答
2

我会将元素存储在一个对象中:

var styles = {};

...

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

您可以轻松删除样式标签:

styles[some_identifier].remove();
于 2013-07-23T22:24:32.560 回答
0
 $('#my_element').removeClass('hidden');
 // also
 $('#my_element').addClass('hidden');
于 2015-10-01T03:36:11.043 回答