jQuery SVG, why can't I addClass?

前端 未结 15 2202
失恋的感觉
失恋的感觉 2020-11-22 02:25

I am using jQuery SVG. I can\'t add or remove a class to an object. Anyone know my mistake?

The SVG:



        
15条回答
  •  感动是毒
    2020-11-22 03:00

    Or just use old-school DOM methods when JQ has a monkey in the middle somewhere.

    var myElement = $('#my_element')[0];
    var myElClass = myElement.getAttribute('class').split(/\s+/g);
    //splits class into an array based on 1+ white space characters
    
    myElClass.push('new_class');
    
    myElement.setAttribute('class', myElClass.join(' '));
    
    //$(myElement) to return to JQ wrapper-land
    

    Learn the DOM people. Even in 2016's framework-palooza it helps quite regularly. Also, if you ever hear someone compare the DOM to assembly, kick them for me.

提交回复
热议问题