简体   繁体   English

如何使用knockout.js设置数据绑定到SVG图像?

[英]How to setup data binding to an SVG image using knockout.js?

I'm trying to get a click event on a SVG to fire using knockout.js: 我正在尝试使用knockout.js在SVG上获取点击事件:

HTML HTML

<img id="the-image" src="img/image.svg" data-bind="????????" />

SVG SVG

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    <rect x="100" fill="#707070" 
            width="20" 
    height="200"
    data-bind="click: $root.open" />
</svg>

this doesn't work if the SVG file is given as source for the img element, it does however work if I just paste it into the img element. 如果SVG文件作为img元素的源给出,这不起作用,但是如果我只是将它粘贴到img元素中它会起作用。

Is there a way to setup the binding so that the data-context is accessible for the SVG ? 有没有办法设置绑定,以便SVG可以访问数据上下文?

SVG elements are not added to the DOM when you use an img element to display the image, therefore knockout.js is unable to bind to those elements. 当您使用img元素显示图像时,SVG元素不会添加到DOM,因此knockout.js无法绑定到这些元素。 The answers to this question contain some solutions that might help you: How do you access the contents of an SVG file in an <img> element? 这个问题的答案包含一些可能对您帮助的解决方案: 如何在<img>元素中访问SVG文件的内容? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM