How to find a specific class name of element when there are multiple classes?

前端 未结 1 1694
别跟我提以往
别跟我提以往 2021-01-16 13:46

I have some DIVs on a page. Each has 3 classes: grid_#, push_# and pull_# where # is an actual number.

I need to replace each class separately, i.e. r

相关标签:
1条回答
  • 2021-01-16 13:58

    If element has multiple classes, you need to use split:

    var old = document.getElementById('elementId');
    var old_class = old.className.split(' ')[0]; // first class
    

    What split does is that it creates an array by splitting string (class in this case) which you can later access by its index eg [0] for first class.

    If element has just one class, you simply use className:

    var cls = old.className;
    
    0 讨论(0)
提交回复
热议问题