我目前正在使用svgJavaScript中的元素.我是新手.
我的问题是我有一个svg元素,我有多个svg:g元素.在我的svg:g元素中,我有各种其他的svg元素.
<svg>
<g class="my-class">
<g class "c1">
<text style="font-size: 9" x="0" y="-4"> john </text>
<text style="font-size: 9" x="70" y="-4"> 13 </text>
</g>
<g class="c2">
<text style="font-size: 9" x="0" y="-4"> john </text>
<text style="font-size: 9" x="70" y="-4"> 13 </text>
</g>
<g class="c3">
<text style="font-size: 9" x="0" y="-4"> john </text>
<text style="font-size: 9" x="70" y="-4"> 13 </text>
</g>
</g>
</svg>
Run Code Online (Sandbox Code Playgroud)
g 动态附加在我的
g"my_class"
现在我想将svg宽度设置为宽度g.my_class宽度.
var svgWidth = $('.my-class').width()
alert(svgWidth); …Run Code Online (Sandbox Code Playgroud) 要在html页面中添加svg图形,通常使用object标签将其包装为:
<object id="svgid" data="mysvg.svg" type="image/svg+xml"
wmode="transparent" width="100" height="100">
this browser is not able to show SVG: <a linkindex="3"
href="http://getfirefox.com">http://getfirefox.com</a>
is free and does it!
If you use Internet Explorer, you can also get a plugin:
<a linkindex="4" href="http://www.adobe.com/svg/viewer/install/main.html">
http://www.adobe.com/svg/viewer/install/main.html</a>
</object>
Run Code Online (Sandbox Code Playgroud)
如果您不在object标签中使用width和height属性,则svg将以完整大小显示.通常我从Open Graphics Library获取svg文件进行测试.有没有办法通过使用JavaScript获得svg的大小?或者我可以只看一下svg xml文件,找出顶级svg标签的大小?
我正在实施以下课程:
export class BbSVGElement extends HTMLElement implements HTMLCanvasElement { ... }
Run Code Online (Sandbox Code Playgroud)
问题是我<svg>在打字稿中找不到元素的类型:<span>元素是HTMLSpanElement,<canvas>元素是HTMLCanvasElement等等。
我问是因为要实现一个简单的任务,比如height()函数,我需要获取元素的边界框。根据这个堆栈溢出答案,这是正确的方法:
height(): number { return this.getBBox().height}
Run Code Online (Sandbox Code Playgroud)
但既然this指的是HTMLElement,当然Property 'getBBox' does not exist on type 'BbSVGElement'.ts(2339)
所以我尝试了各种铸件;像:
return (<SVGElement> this).getBBox().width
return (<SVGSVGElement> this).getBBox().width
Run Code Online (Sandbox Code Playgroud)
我当然可以做类似的事情
return (<any> this).getBBox().width
Run Code Online (Sandbox Code Playgroud)
或者
return this.offsetWidth // <-- This property here comes from HTMLElement
Run Code Online (Sandbox Code Playgroud)
但如果可能的话,我宁愿不要。
那么:正确的演员阵容应该怎么做?还是我错过了另一个选择?