Remove classname from element with javascript

前端 未结 4 2012
忘掉有多难
忘掉有多难 2020-12-24 07:35

I found the following regex from another Stack Overflow question: Change an element's class with JavaScript

And have used it in part of my script with success, h

4条回答
  •  粉色の甜心
    2020-12-24 08:33

    That's because replace doesn't actually modify the string you call it on; rather, it returns a new string. So:

         foo.className = foo.className.replace( /(?:^|\s)bar(?!\S)/ , '' )
    

    (By the way, you don't actually need to do this in raw JavaScript, since jQuery objects offer a removeClass method: http://api.jquery.com/removeClass/. So you could write:

         $('#foo').removeClass('bar');
    

    or:

         $(foo).removeClass('bar');
    

    )

提交回复
热议问题