简体   繁体   English

通过Javascript访问SVG元素

[英]access SVG elements via Javascript

This is a follow up to a question asked before at: How to access SVG elements with Javascript However, the solution does not seem to work for me. 这是前面提到的问题的后续问题: 如何使用Javascript访问SVG元素但是,该解决方案似乎对我不起作用。 I am testing on the latest version of Chrome. 我正在测试最新版本的Chrome。 I have a map of the US as an SVG file, which I have downloaded on my machine and made some changes to the xml code. 我有一张美国地图作为SVG文件,我已经在我的机器上下载并对xml代码进行了一些更改。

I have the svg embedded using the object tag and assigned an id of "USAsvg" and am proceeding with baby-steps first. 我使用object标签嵌入了svg并分配了一个“USAsvg”的id,并且首先进行了婴儿步骤。 For a button onclick event I am executing the following code without success. 对于按钮onclick事件,我执行以下代码但没有成功。 Here 'CA' is a state declared using the path tag within the svg file. 这里'CA'是使用svg文件中的path标签声明的状态。

var a = document.getElementById('USAsvg');
var svgDoc = a.contentDocument; 
var delta = svgDoc.getElementById('CA');

alert(delta.value);

this works in the latest chrome, load the SVG into your DOM directly, then manipulate it as if it were a normal html node, the html() function in jQuery does not work, use text() instead. 这适用于最新的chrome,直接将SVG加载到您的DOM中,然后操纵它就好像它是一个普通的html节点,jQuery中的html()函数不起作用,而是使用text()。

$('.your_div_for_svg').load('svg/file.svg', function(){
      $('#some_textnode_w_id_within_svg').text('Hello word');
});

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

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