apply CSS style to particular elements dynamically

前端 未结 5 2069
我在风中等你
我在风中等你 2021-01-15 19:56

I have a div with paragraphs inside:

...

...

I want dynamically to apply

5条回答
  •  春和景丽
    2021-01-15 20:48

    With CSS you can define a child selector.

    Building on that, you can dynamically add/remove a style class to your div and the style will apply to the children you specify in that selector.

    Let's assume you want to apply this to a specific div, not any/every one. So give the target div an identifier:

    ...

    ...

    Adding a dynamic style with simple javascript:

    document.getElementById('foo').style.className='dynamic_style'
    

    The CSS (using combined id and class on a child selector):

    div#foo.dynamic_style > p { /* your style definition*/ }
    

提交回复
热议问题