简体   繁体   English

交互式SVG在HTML中,正确的方式

[英]Interactive SVG in html, the right way

I need to include a SVG image(a map) in my html doc, and hopefully I can use JQuery to manipulate it. 我需要在我的html文档中包含一个SVG图像(一个地图),希望我可以使用JQuery来操作它。 I looked this up on the Internet and some say you can't manipulate the SVG directly. 我在互联网上看了这个,有些人说你不能直接操纵SVG。 Some suggested the JQuery SVG plugin by Keith Wood, but I can't open that link . 有人建议Keith Wood使用JQuery SVG插件,但我无法打开该链接 Here is an example says that you can include SVG code directly into html5. 这是一个例子,说明你可以将SVG代码直接包含在html5中。 Well for one thing, my SVG code is a little long so it looks awful if I put the code there, for another I'm still not sure how to manipulate it even if I do that. 好吧,有一件事,我的SVG代码有点长,所以如果我把代码放在那里看起来很糟糕,另一方面,即使我这样做,我仍然不确定如何操纵它。

So I think maybe I can use some framework, and I tried Raphael, but I need me to convert my SVG to another form and Raphael would render the image for me. 所以我想也许我可以使用一些框架,我试过拉斐尔,但我需要我将我的SVG转换为另一种形式,拉斐尔会为我渲染图像。 But strangely the image became much smaller after I changed the SVG image. 但奇怪的是,在我改变SVG图像后,图像变小了。 And I have trouble figuring out what's wrong. 我无法弄清楚出了什么问题。

What I want to achieve is very similar to this page in the admob.com. 我想要实现的与admob.com上的这个页面非常相似。 This page seems to include the SVG code directly in the page. 此页面似乎直接在页面中包含SVG代码。

Can anyone give some advice to achieve something like that? 任何人都可以提出一些建议来实现这样的目标吗?

Thanks. 谢谢。

You don't actually need jQuery to edit the SVG (but you can use it). 你实际上并不需要 jQuery来编辑SVG(但你可以使用它)。 Include the SVG doc using the object tags like so: 使用对象标签包含SVG doc,如下所示:

<object data="mySVG.svg" type="image/svg+xml" id="mySVG"></object>

You can then manipulate the DOM of the SVG by getting the document like so: 然后,您可以通过获取document来操纵SVG的DOM:

var svgObject = document.getElementById("mySVG");

moSVG.onload = function(){
    var svgDoc = moSVG.contentDocument;
    //Do SVG manipulation here, using svgDoc instead of document
}

Note the use of onload . 注意使用onload Without it, you're likely to get errors as the JS runs before the SVG has fully loaded (especially if you use images in the SVG). 没有它,你可能会在SVG完全加载之前运行JS(特别是如果你在SVG中使用图像)。

Once you're sure the SVG's loaded, you can use it in JavaScript outside of the onload statement. 一旦确定加载了SVG,就可以在onload语句之外的JavaScript中使用它。

If, on the other hand, you want to embed JavaScript inside the SVG directly, you can use <script> tags as usual with one caveat: you need to use xlink if you want external JS (like the following example that imports jQuery) 另一方面,如果你想直接在SVG中嵌入JavaScript,你可以像往常一样使用<script>标签,但有一点需要注意:如果你想要外部JS,你需要使用xlink(如下面的导入jQuery的例子)

<script type="text/ecmascript" xlink:href="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"/>

Internal JS can be described as follows: 内部JS可以描述如下:

If you decided to use this, make sure to define xlink in the opening <svg> tag like so (don't change the urls): 如果你决定使用它,请确保在开头的<svg>标签中定义xlink (不要更改网址):

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

However, remember that the JS that's directly in the SVG cannot see anything on your HTML page. 但是,请记住,这是直接在SVG的JS 看不到你的HTML页面上的任何内容。

I've voted up @SomeKittens' answer as that is the right way to do what you are looking for, but an alternative would be Ready Set Raphael : it gives you the Raphael code for your SVG and then you can use it like any other Raphael JS. 我已经投了@SomeKittens的答案,因为这是你正在寻找的正确方法,但另一种选择就是Ready Set Raphael :它为你的SVG提供Raphael代码然后你可以像其他任何一样使用它拉斐尔JS。 (Obviously you should have included Raphael to use this). (显然你应该包括Raphael使用它)。 You'll still have to figure out variables names if you want to reference independent elements. 如果要引用独立元素,则仍需要确定变量名称。

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

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