从主html脚本调用SVG的外部JavaScript函数吗? [英] Call SVG's external JavaScript function from the main html script?

查看:272
本文介绍了从主html脚本调用SVG的外部JavaScript函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个类似的问题,但是我想知道当SVG的JavaScript函数未嵌入但是从外部文件导入时如何访问它们的.

This is a similar question but I would like to know how to access the SVG's JavaScript functions when they are not embedded but are imported from an external file:

在html javascript函数内部调用svg javascript函数

因此,我将尝试阐明我的问题,希望不要太困惑.

So, I'll attempt to make my question clear and hopefully not too convoluted.

我有main.html和main.js.

I have main.html and main.js.

我也有someSVG.svg,它导入了具有功能foo()的someSVG.js.

I also have someSVG.svg which imports someSVG.js which has a function, foo().

main.html导入someSVG.svg作为对象元素.

main.html imports someSVG.svg as an object element.

如何从main.js调用foo()?

How do I call foo() from main.js?

推荐答案

如果您将SVG嵌入<object>标签,则可以使用

Provided you embed the SVG with an <object> tag, you can access the window object of the SVG with

var inner = document.querySelector('object').contentWindow

就像外部窗口的窗口对象一样,它提供了在普通窗口中可以找到的所有全局变量,例如脚本定义的inner.foo()inner.document.

Just like the window object of the outer window, it provides all global variables that you would find in normal window, for example a script-defined inner.foo() or inner.document.

注意:Safari支持版本10的HTMLObjectElement.contentDocument,但不支持.documentWindow.

NOTE: Safari supports HTMLObjectElement.contentDocument from version 10, but not .documentWindow.

这篇关于从主html脚本调用SVG的外部JavaScript函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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