jQuery CSS() for dynamically created elements

前端 未结 3 2080
面向向阳花
面向向阳花 2020-12-03 08:06

I\'m using jQuery CSS function to style some elements

$element.css(style);

This works, but a part of the elements are created dynamically a

3条回答
  •  自闭症患者
    2020-12-03 08:35

    There's no event for elements created (not universally available, anyway). You could

    • Add the rules to a stylesheet so that they are automatically applied to the newly created elements
    • Chain the css() method when you create your elements:

      $('')
          .appendTo(document.body)
          .css(style);
      
    • Create a new stylesheet dynamically:

      $("