我有以下javascript代码,用于操作通过object标签插入的SVG文档,它可以在Chrome,FireFox以及iOS Safari中正常运行。
但是,当我在Microsoft Edge或IE上运行时,我收到以下错误。
"对象不支持属性或方法' beginElement'"
有关如何使其适用于Edge的任何想法吗?
PS:尝试添加代码,以便它可以在这里运行,但失败了,不知道为什么。
var object = document.getElementById("basicsvg");
object.onload=function() {
var svgdoc;
// get access to the SVG document object
try {
svgdoc = object.contentDocument;
} catch(e) {
try {
svgdoc = object.getSVGDocument();
} catch (e) {
alert("SVG in object not supported in your environment");
}
}
if (!svgdoc) return;
var r = svgdoc.rootElement;
// get SVG element and modify
var back = svgdoc.getElementById('back');
var base = svgdoc.getElementById('base');
back.onclick = bback;
base.onclick = bback;
function bback() {
//SVG supports namespaces
// var width = parseFloat(square.getAttributeNS(null,"width"));
// width-=50;
// square.setAttributeNS(null,"width",width);
var color = back.getAttributeNS(null,"fill");
if (color == "#32A3D8") {
back.setAttributeNS(null,"fill","#DADAD7");
back.setAttributeNS(null,"stroke","#DADAD7");
base.setAttributeNS(null,"fill", "#32A3D8");
base.setAttributeNS(null,"stroke","#32A3D8");
} else {
back.setAttributeNS(null,"fill","#32A3D8");
back.setAttributeNS(null,"stroke","#32A3D8");
base.setAttributeNS(null,"fill","#DFE3DE");
base.setAttributeNS(null,"stroke","#DFE3DE");
}
}
var lbasic = svgdoc.getElementById('lbasic');
var rbasic = svgdoc.getElementById('rbasic');
lbasic.onclick = lwindow;
rbasic.onclick = rwindow;
function lwindow() {
var windowcolor = lbasic.getAttributeNS(null, "fill");
if (windowcolor == "#32A3D8") {
lbasic.setAttributeNS(null, "fill", "#80b71b");
} else {
lbasic.setAttributeNS(null, "fill", "#32A3D8");
}
}
function rwindow(){
var windowcolor = rbasic.getAttributeNS(null, "fill");
if (windowcolor == "#32A3D8") {
rbasic.setAttributeNS(null, "fill", "#80b71b");
} else {
rbasic.setAttributeNS(null,"fill", "#32A3D8");
}
}
// START OF INCORPORATE ROTATING ASPECT TO GEARS + PAUSE OR STOP FEATURE
var gear3 = svgdoc.getElementById('gear-3');
var gear4 = svgdoc.getElementById('gear-4');
gear3.onclick = rotategears;
gear4.onclick = rotategears;
var gears;
function rotategears(){
var SVGRoot
if (gears == "startgears"){
// svgdoc.pauseAnimations();
// gear3.pauseAnimations();
gears = "stopgears";
// gear3.setAttributeNS(null, "dur", "0");
// gear4.setAttributeNS(null, "dur", "0");
addRotateTransform('gear-3', 0, -1);
addRotateTransform('gear-4', 0, 1);
object.pauseAnimation();
} else {
addRotateTransform('gear-3', 5, -1);
addRotateTransform('gear-4', 6, 1);
gears = "startgears";
}
}
function addRotateTransform(target_id, speed, direction) {
var svgNS = "http://www.w3.org/2000/svg";
var element_to_rotate = svgdoc.getElementById(target_id);
var my_transform = svgdoc.createElementNS(svgNS, "animateTransform");
var bb = element_to_rotate.getBBox();
var cx = bb.x + bb.width/2;
var cy = bb.y + bb.height/2;
my_transform.setAttributeNS(null, "attributeName", "transform");
my_transform.setAttributeNS(null, "attributeType", "XML");
my_transform.setAttributeNS(null, "begin", "target_id.click");
my_transform.setAttributeNS(null, "type", "rotate");
my_transform.setAttributeNS(null, "dur", speed + "s");
my_transform.setAttributeNS(null, "fill", "freeze");
my_transform.setAttributeNS(null, "end", "target_id.click");
my_transform.setAttributeNS(null, "repeatCount", "indefinite");
my_transform.setAttributeNS(null, "from", "0 "+cx+" "+cy);
my_transform.setAttributeNS(null, "to", 360*direction+" "+cx+" "+cy);
element_to_rotate.appendChild(my_transform);
my_transform.beginElement();
}
// END OF INCORPORATE ROTATING ASPECT TO GEARS + PAUSE OR STOP FEATURE
// Hover function library
// In order to select by class atribute, it is important to note that ElementS is plurarl because there may be several instances of the class.
// Further more the value that is brought back is an array.
function basicHover (target_class, speedH, fromColor, toColor) {
var svgNS = "http://www.w3.org/2000/svg";
var element_to_rotate = svgdoc.getElementsByClass(target_id);
var my_transform = svgdoc.createElementNS(svgNS, "animateTransform");
my_transform.setAttributeNS(null, "attributeName", "fill");
my_transform.setAttributeNS(null, "attributeType", "XML");
my_transform.setAttributeNS(null, "begin", "mouseover");
my_transform.setAttributeNS(null, "dur", speedH + "s");
my_transform.setAttributeNS(null, "fill", "freeze");
my_transform.setAttributeNS(null, "end", "target_id.click");
my_transform.setAttributeNS(null, "repeatCount", "indefinite");
my_transform.setAttributeNS(null, "from", fromColor);
my_transform.setAttributeNS(null, "to", toColor);
element_to_rotate.appendChild(my_transform);
my_transform.beginElement();
}
// Add opacity transform
var bulb = svgdoc.getElementById('bulb');
var bulbs = "pauseOpacity";
bulb.onclick = bulbOpacity;
function bulbOpacity(){
if (bulbs === "pauseOpacity") {
bulbs = "startOpacity";
addOpacityTransform('bulb', 6, 1, 0.3);
} else {
bulbs = "pauseOpacity";
addOpacityTransform('bulb', 6, 1, 1);
}
}
function addOpacityTransform (target_id, speed, start_opacity, end_opacity){
var element_to_animate_opacity = svgdoc.getElementById(target_id);
var svgNS = "http://www.w3.org/2000/svg";
var my_transform_opacity = svgdoc.createElementNS(svgNS, "animate");
my_transform_opacity.setAttributeNS(null, "attributeName", "opacity");
my_transform_opacity.setAttributeNS(null, "values", start_opacity+"; "+end_opacity+"; "+start_opacity);
my_transform_opacity.setAttributeNS(null, "dur", speed + "s");
my_transform_opacity.setAttributeNS(null, "repeatCount", "indefinite");
element_to_animate_opacity.appendChild(my_transform_opacity);
my_transform_opacity.beginElement();
}
};

<div style="width=211px;">
<svg id="basicsvg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="211.8" height="181.4" viewBox="0 0 211.8 181.4">
<path id="back" fill="#32A3D8" d="M168.2 139.4h-31c-.7-2-1.3-4.6-.4-6.9.3-.7.8-1.4 1.4-2h37v-5.8h-14.6c-1-1-2.4-2.8-3.9-6.1 1.7-4.4.9-9.2.3-11.8h43.5c6.1 0 11.3-5.5 11.3-11.6V16.5c0-6.1-5.2-11.3-11.3-11.3H82.1c-6.1 0-10.8 5.3-10.8 11.3v78.6c0 6.1 4.7 11.6 10.8 11.6h71.6c.4 1.3 1 4.1.8 7.1-.3-.3-.6-.5-1-.5-.8-.1-6.5-.1-12.1-.1h-8.3c-2 0-3.4 0-3.7.1-1.6.1-2.5 3.4-2.5 3.4-1.7 4.5-3.4 6.8-4.6 7.9h-14.5v5.8h26.6c-.1.2-.3.5-.4.7-1.2 2.8-.8 5.7-.1 8.1h-26.5c-5.1 0-8.9 4.3-8.9 9.4V173c0 5 3.8 8.3 8.9 8.3h61c5.1 0 7.8-3.2 7.8-8.3v-24.4c-.2-4.9-3-9.2-8-9.2zm3.1-62.9l-13.8-17.3L144 77l13.5-17.8-14.3-17.9 14.3 17.9 14.5-19-14.5 19 13.8 17.3zM94.5 59.1l14-18.1-14 18.1L107.9 76 94.5 59.1 81.2 76.5l13.3-17.4-14.2-18 14.2 18zm-35.1 54.3v-1.1c0-8.7-5-16.9-15.6-16.9-8.7 0-11.8 5-14.3 10.5-1.4-1.2-3.1-1.9-5-1.9-4.3 0-7.8 3.5-7.8 7.9 0 .5 0 .9.1 1.3-5.9.9-10.4 6.1-10.4 12.3 0 6.9 5.5 12.4 12.3 12.4H57c6.8 0 12.3-5.6 12.3-12.4 0-5.9-4.2-10.9-9.9-12.1z"/>
<path fill="#DADAD7" d="M71.7 6h131.2v90.3H71.7z"/>
<path fill="#E3F0E6" d="M71.7 6h131.2v14.8H71.7z"/>
<path fill="#152C34" d="M195.3 0H76.9C70.8 0 66 5.3 66 11.3v78.6c0 6.1 4.7 11.6 10.8 11.6h118.4c6.1 0 11.3-5.5 11.3-11.6V11.3c.1-6-5.1-11.3-11.2-11.3zm3.5 92.4h-125V9.1h125v83.3z"/>
<path fill="#FBFCFC" d="M155.4 119.5H170v5.8h-67.6v-5.8h14.5c1.2-1.2 2.9-3.5 4.6-7.9 0 0 .9-3.3 2.5-3.4.8-.1 6.5-.1 12.1-.1s11.4 0 12.1.1c1.6.1 2.5 3.4 2.5 3.4 1.8 4.4 3.5 6.7 4.7 7.9z"/>
<path id="base" fill="#DFE3DE" d="M155.4 119.5c-1.2-1.2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2-.1h.2v.1c.1.2 0 .4-.1.6zm3.2-.2c0 .3-.3.6-.7.7.1-.7.3-1.2.3-1.2h.2c.1 0 .2 0 .2.4v.1z"/>
<path fill="#FFFAF0" d="M153.6 15c0 1.6-1.3 2.9-2.9 2.9h-55c-1.6 0-2.9-1.3-2.9-2.9 0-1.6 1.3-2.9 2.9-2.9h55.1c1.5 0 2.8 1.3 2.8 2.9zm41.9 0c0 1.6-1.3 2.9-2.9 2.9h-26c-1.6 0-2.9-1.3-2.9-2.9 0-1.6 1.3-2.9 2.9-2.9h26c1.6 0 2.9 1.3 2.9 2.9z"/>
<path fill="#3E442B" d="M76.7 31.1v-2.5h118.9v2.5H76.7"/>
<path id="lbasic" fill="#32A3D8" d="M76.8 37.3H102v33H76.8z"/>
<path fill="#3E442B" d="M76.8 73.4h25.3v3H76.8zm0 5.3h25.3v3H76.8zm0 6.3h25.3v3H76.8zm28.1-47.5h27.7v3h-27.7zm0 5.3h27.7v3h-27.7zm0 6.3h27.7v3h-27.7zm0 5.6h27.7v3h-27.7zm0 5.3h27.7v3h-27.7zm0 6.3h27.7v3h-27.7zm0 7h27.7v3h-27.7zm0 5.3h27.7v3h-27.7zm0 6.3h27.7v3h-27.7zm34.9-11.5h25.3v3h-25.3zm0 5.3h25.3v3h-25.3zm0 6.3h25.3v3h-25.3zm28-47.5h27.7v3h-27.7zm0 5.3h27.7v3h-27.7zm0 6.3h27.7v3h-27.7zm0 5.6h27.7v3h-27.7zm0 5.3h27.7v3h-27.7zm0 6.3h27.7v3h-27.7zm0 7h27.7v3h-27.7zm0 5.3h27.7v3h-27.7zm0 6.3h27.7v3h-27.7z"/>
<circle fill="#32A3D8" cx="78.7" cy="15.3" r="2.6"/>
<circle fill="#32A3D8" cx="159.1" cy="15.3" r="2.6"/>
<circle fill="#32A3D8" cx="86.5" cy="15.3" r="2.6"/>
<path fill="none" stroke="#DADAD7" d="M103.3 35.8L76 71.3m26.6-.5L75.1 36"/>
<path id="rbasic" fill="#32A3D8"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