最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

HTMl+SVG+JavaScript: change text at runtime? - Stack Overflow

programmeradmin3浏览0评论

I am about to migrate a few Flash-based applications to HTML+JavaScript+SVG (the single target rendering engine is Webkit).

I'm pletely new to SVG and I'd like to know if I can use a SVG as a template image to fill the screen and change contained text on the fly from the JavaScript code embedded in the HTML page.

What I want to do is: draw the basic structure of the page in Inkscape (with some graphics and text placeholders) and then just display the SVG in the HTML page and fill the text placeholders via JavaScript.

I could get the same result by displaying a static SVG image in the background and place some absolutely positioned DIVs on top of it to display the text, but I'd like to design the position, size and style of the text labels from within Inkscape.

Can this be done? How?

I'm using the Prototype framework, not JQuery.

I am about to migrate a few Flash-based applications to HTML+JavaScript+SVG (the single target rendering engine is Webkit).

I'm pletely new to SVG and I'd like to know if I can use a SVG as a template image to fill the screen and change contained text on the fly from the JavaScript code embedded in the HTML page.

What I want to do is: draw the basic structure of the page in Inkscape (with some graphics and text placeholders) and then just display the SVG in the HTML page and fill the text placeholders via JavaScript.

I could get the same result by displaying a static SVG image in the background and place some absolutely positioned DIVs on top of it to display the text, but I'd like to design the position, size and style of the text labels from within Inkscape.

Can this be done? How?

I'm using the Prototype framework, not JQuery.

Share Improve this question edited May 30, 2011 at 15:34 robertc 75.7k19 gold badges200 silver badges180 bronze badges asked May 30, 2011 at 9:02 Udo GUdo G 13.1k15 gold badges62 silver badges92 bronze badges 1
  • Use html5 'inline svg' with <svg></svg>. – Jan Commented May 31, 2011 at 6:44
Add a ment  | 

3 Answers 3

Reset to default 6

Try jQuery. Set manually an ID to an SVG-TextNode, and then try:

$("#my_id").textContent = "new Value";

May be it works. Otherwise try:

document.getElementById("my_id").textContent = "new Value";

How you access the SVG depends on exactly how you include it in your page. You can either use and object or embed element and link to the SVG, or include it inline in the page. Note that including it via an img element won't work - the SVG will be treated as an image 'black box', you won't be able to access the internal structure.

Embedding via an object is straightforward and will work in all browsers which support SVG:

<object id="svg_object" width="672" height="243" data="myimage.svg" type="image/svg+xml">
    No SVG support
</object>

Then to get at the internal structure you need to get the contentDocument:

var svg = $('svg_object').contentDocument;
var svgel = svg.getElementById('myid');

Note that the internal DOM of the SVG won't be automatically extended by prototype.js in this case, so you'll have to fall back on regular DOM methods.

Embedding the SVG directly in the page will work if you serve your page as application/xhtml+xml or if the browser has an HTML5 pliant parser (Firefox 4, Chrome, IE9 but not Opera or earlier Firefox). However, now your prototype.js methods will work directly with the SVG elements, making certain things much more simple:

var svgel = $('myid');

I've done a couple of examples: object, inline. They work for me in Firefox 4, you may need to do some messing around to get them working in other browsers (with the caveats mentioned above as far as support is concerned).

SVG is a XML based document, that you can manipulate with javascript just like you manipulate XHTML documents.

getElementById will work with SVG the same way it work with xhtml, so you'll be hable to change text in a SVG dynamically.

发布评论

评论列表(0)

  1. 暂无评论