使用jQuery设置viewBox属性 [英] Setting viewBox attribute with jQuery

查看:102
本文介绍了使用jQuery设置viewBox属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有一个< svg> 元素,并想给它一个 viewBox 属性。当我尝试使用jQuery时,如下所示:

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');

几乎有效,但它给元素一个view b ox属性(注意小写'b')。这个属性要求驼峰工作,至少在我测试它的Chrome中。有没有解决方法?

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?

推荐答案

我使用@ Mat的原生Javascript setAttribute 小费,

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

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

这篇关于使用jQuery设置viewBox属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆