Setting viewBox attribute with jQuery [duplicate]

2020-03-01 20:06发布

I have an <svg> element on my page and would like to give it a viewBox attribute. When I try this with jQuery, like so:

$('svg').attr('viewBox', '0 0 800 400');

It almost works, but it gives the element a "viewbox" attribute (notice the lower case 'b'). This attribute requires the camel case to work, at least in Chrome where I have tested it. Are there any workarounds?

1条回答
时光不老,我们不散
2楼-- · 2020-03-01 20:33

I solved this using @Mat's native Javascript setAttribute tip,

$('svg').removeAttr('viewBox');
$('svg').each(function () { $(this)[0].setAttribute('viewBox', '0 0 800 400') });
查看更多
登录 后发表回答