简体   繁体   English

svg onload函数何时发生

[英]when does the svg onload function happen

Given this: 鉴于这种:

<?xml version="1.0" standalone="yes"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100px" height="100px" version="1.1"
 xmlns="http://www.w3.org/2000/svg"
>
<text 
 x="20" 
 y="20" 
 onload="alert('load'); setAttribute('fill', 'fuchsia')"
 onclick="setAttribute('fill', 'lightgreen')"
 onmouseout="setAttribute('fill', 'black')"
>Load me</text>
</svg>

I would expect to see pink text when the svg was opened. 当svg打开时,我希望看到粉红色的文本。 onclick and onmouseout work as expected. onclick和onmouseout可以正常工作。

This doesn't happen in firefox. 在Firefox中不会发生这种情况。 IE can't open it, period. IE无法打开它,期间。

Any explanations? 有什么解释吗?

Use onload event on <svg> element. <svg>元素上使用onload事件。 This works fine on all browsers. 在所有浏览器上都可以正常工作。

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<svg onload="init(evt)" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gui="http://www.kevlindev.com/gui">
    <script>var info,infoElem;

    function init(e) {
        if ( window.svgDocument == null )
            svgDocument = e.target.ownerDocument;

        infoElem = svgDocument.getElementById("info");
        infoElem.setAttributeNS(null,"fill", "fuchsia");
    }

    function changeColor(c){
        infoElem.setAttributeNS(null,"fill", c);
    }</script>
    <text id="info" x="20"  y="20" onclick="changeColor('lightgreen')" onmouseout="changeColor('black')">Load me</text>
</svg>

This worked for me 这对我有用

//snip...

    <svg width="100px" height="100px" version="1.1" onload="alert('load'); setAttribute('fill', 'fuchsia')"
     xmlns="http://www.w3.org/2000/svg"
    >

//snip...

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

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