Creating a CSS class in jQuery

前端 未结 4 2136
[愿得一人]
[愿得一人] 2020-12-04 14:37

I believe the .addClass() function in jQuery attaches a CSS class to the current selection, but I was wondering I could create or define a CSS class in jQuery, and then atta

4条回答
  •  遥遥无期
    2020-12-04 14:59

    With jQuery.Rule you can write code like this to append a new CSS rule:

    $.rule('#content ul{ border:1px solid green }').appendTo('style');
    

    Extending a rule:

    $.rule('#content ul', 'style').append('background:#FF9');
    

    Removing the whole rule:

    $.rule('#content ul', 'style').remove();
    

    There is more in the API docs.

    Internally, it uses the "append stylesheet to head" trick that MooGoo mentioned as well.

提交回复
热议问题