javascript 使用 jQuery 设置 viewBox 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24913270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:34:57  来源:igfitidea点击:

Setting viewBox attribute with jQuery

javascriptjquerysvg

提问by Ben

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

我的<svg>页面上有一个元素,想给它一个viewBox属性。当我用 jQuery 尝试这个时,像这样:

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

It almostworks, 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?

几乎可以工作,但它为元素提供了“view box”属性(注意小写的“b”)。这个属性需要驼峰式外壳才能工作,至少在我测试过的 Chrome 中是这样。有什么解决方法吗?

回答by Ben

I solved this using @Mat's native Javascript setAttributetip,

我使用@Mat 的原生 JavascriptsetAttribute技巧解决了这个问题,

$('svg').removeAttr('viewBox');
$('svg').each(function () { $(this)[0].setAttribute('viewBox', '0 0 800 400') });