css - jQuery SVG, why can't I addClass? -
i using jquery svg. can't add together or remove class object. know mistake?
the svg:
<rect class="jimmy" id="p5" x="200" y="200" width="100" height="100" /> the jquery won't add together class:
$(".jimmy").click(function() { $(this).addclass("clicked"); }); i know svg , jquery working fine because can target object , fire alert when it's clicked:
$(".jimmy").click(function() { alert('handler .click() called.'); });
jquery can't add together class svg.
.attr() works svg, if don't want depend on library:
// instead of .addclass("newclass") $("#item").attr("class", "oldclass newclass"); // instead of .removeclass("newclass") $("#item").attr("class", "oldclass"); and if don't want depend on jquery:
var element = document.getelementbyid("item"); // instead of .addclass("newclass") element.setattribute("class", "oldclass newclass"); // instead of .removeclass("newclass") element.setattribute("class", "oldclass"); edit 2016: read next 2 answers.
element.classlist.add('newclass') works in modern browsers jquery 3 fixes underlying issue jquery css svg addclass jquery-svg
No comments:
Post a Comment