gpt4 book ai didi

javascript - 获取嵌入 html 文件的 svg 坐标的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-28 16:24:55 25 4
gpt4 key购买 nike

我将 svg 嵌入到 html 文件中,如下所示:

    <embed src="mysvg.svg" type="image/svg+xml" />

svg 文件中有一个 id 为“lot1a”的矩形,有什么方法可以获取lot1a 的坐标并将其保存在变量中并将其用作另一个 svg 文件的坐标。我的主要问题是,正如 Phrogz 所说,主机 HTML(或 SVG)无法访问嵌入文件的 DOM。那我能做什么呢?

最佳答案

This should work (至少在 Firefox 和 Opera 中):

window.onload = function () {
var svgel = document.getElementById('myembed');
var svg;
if (typeof svgel.getSVGDocument !== 'undefined') {
svg = svgel.getSVGDocument();
} else {
svg = svgel.contentDocument;
}
var rect = svg.getElementById('lot1a');
window.alert('x:' + rect.getAttribute('x') + ' y:' + rect.getAttribute('y'));
}

Chrome has a bug ,但你可以try these workarounds .

关于javascript - 获取嵌入 html 文件的 svg 坐标的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8320927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com