How do I add a class to a given element?

前端 未结 25 2449
清酒与你
清酒与你 2020-11-21 11:34

I have an element that already has a class:

25条回答
  •  一个人的身影
    2020-11-21 11:56

    Assuming you're doing more than just adding this one class (eg, you've got asynchronous requests and so on going on as well), I'd recommend a library like Prototype or jQuery.

    This will make just about everything you'll need to do (including this) very simple.

    So let's say you've got jQuery on your page now, you could use code like this to add a class name to an element (on load, in this case):

    $(document).ready( function() {
      $('#div1').addClass( 'some_other_class' );
    } );
    

    Check out the jQuery API browser for other stuff.

提交回复
热议问题