جافاسكريبت - متى تحدث وظيفة onload في SVG
بالنظر إلى هذا:
<?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>
كنت أتوقع رؤية نص وردي عند فتح ملف SVG. تعمل أحداث onclick و onmouseout كما هو متوقع.
هذا لا يحدث في فايرفوكس. الإنترنت إكسبلورر لا يمكنه فتحه، نقطة.
أي تفسيرات؟
الإجابة رقم 1
استخدم حدث onload على عنصر <svg>. هذا يعمل بشكل جيد في جميع المتصفحات.
<?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>
الإجابة رقم 2
هذا نجح معي
//snip...
<svg width="100px" height="100px" version="1.1" onload="alert('load'); setAttribute('fill', 'fuchsia')"
xmlns="http://www.w3.org/2000/svg"
>
//snip...
