Only on versions 2 Utile 2

It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.

Before jQuery version 1.12/2.2, the .addClass() method manipulated the className property of the selected elements, not the class attribute. Once the property was changed, it was the browser that updated the attribute accordingly. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).

As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the class attribute is used instead. So, .addClass() can be used on XML or SVG documents.

Sub Item Title

  • Content 01

    It’s important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.

    Before jQuery version 1.12/2.2, the .addClass() method manipulated the className property of the selected elements, not the class attribute. Once the property was changed, it was the browser that updated the attribute accordingly. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).

    As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the class attribute is used instead. So, .addClass() can be used on XML or SVG documents.

    
    																								

    <!doctype html>

    <html lang=”en”>

    <head>

    <meta charset=”utf-8 PHP “>

    <title>addClass demo</title>

    <style> p { margin: 8px; font-size: 16px; } .selected { color: blue; } .highlight { background: yellow; } </style>

    <script src=”https://code.jquery.com/jquery-3.5.0.js”></script></head><body> <p>Hello</p><p>and</p><p>Goodbye</p> <script>$( “p” ).last().addClass( “selected” );</script>

    </body></html>

    
    																								

    <! doctype html>

    <html lang=”en”>

    <head>

    <meta charset=”utf-8 JSON “>

    <title>addClass demo</title>

    <style> p { margin: 8px; font-size: 16px; } .selected { color: blue; } .highlight { background: yellow; } </style>

    <script src=”https://code.jquery.com/jquery-3.5.0.js”></script></head><body> <p>Hello</p><p>and</p><p>Goodbye</p> <script>$( “p” ).last().addClass( “selected” );</script>

    </body></html>

Privacy Preference Center