SVG是一種以XML定義二維向量圖現的圖形格式,由W3C制定,是一個開啟的標準。
例如有一個001.svg的圖片如下:
以記事本開啟001.svg,可以發現是以一般文字來定義圖形。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width="252.80942"
height="100.89236"
id="svg2">
<defs
id="defs4" />
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
transform="translate(-105.26811,-111.91601)"
id="layer1">
<rect
width="251.91707"
height="100"
rx="5.5981574"
ry="50"
x="105.71429"
y="112.36218"
id="rect2985"
style="fill:#008000;stroke:#000000;stroke-width:0.89235312;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />
<text
x="151.42857"
y="178.07648"
id="text2987"
xml:space="preserve"
style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"><tspan
x="151.42857"
y="178.07648"
id="tspan2989">文字測試</tspan></text>
</g>
</svg>
SVG可以像一般的圖片以<img>的Tag或直接內嵌到HTML5來呈現,例如下列測試的網頁TEST01.html:
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>TEST SVG</title>
</head>
<body>
<img src="001.svg"></img>
<br />
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width="252.80942"
height="100.89236"
id="svg2">
<defs
id="defs4" />
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
transform="translate(-105.26811,-111.91601)"
id="layer1">
<rect
width="251.91707"
height="100"
rx="5.5981574"
ry="50"
x="105.71429"
y="112.36218"
id="rect2985"
style="fill:#008000;stroke:#000000;stroke-width:0.89235312;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />
<text
x="151.42857"
y="178.07648"
id="text2987"
xml:space="preserve"
style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"><tspan
x="151.42857"
y="178.07648"
id="tspan2989">文字測試</tspan></text>
</g>
</svg>
</body>
</html>
結果如下圖,可以發現兩張一模一樣的圖片在測試網頁上。
因此我試著以jQuery來動態更改SVG上的文字,程式碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<title>Hello World</title>
</head>
<body>
<img src="button.png" id="imgBtn1" />
<br />
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width="252.80942"
height="100.89236"
id="svg2">
<defs id="defs4" />
<metadata id="metadata7">
<rdf:RDF>
<cc:Work rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
transform="translate(-105.26811,-111.91601)"
id="layer1">
<rect
width="251.91707"
height="100"
rx="5.5981574"
ry="50"
x="105.71429"
y="112.36218"
id="rect2985"
style="fill:#008000;stroke:#000000;stroke-width:0.89235312;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />
<text
x="151.42857"
y="178.07648"
id="text2987"
xml:space="preserve"
style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans">
<tspan
x="151.42857"
y="178.07648"
id="tspan2989">文字測試</tspan></text>
</g>
</svg>
</body>
</html>
<script src="jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$("#imgBtn1").click(function() {
alert(SVG_Check());
$('#tspan2989').text("改變成功");
});
//判斷瀏覽器是否支援SVG
function SVG_Check(){
SVG_NS = 'http://www.w3.org/2000/svg' ;
return !!document.createElementNS && !!document.createElementNS(SVG_NS, 'svg').createSVGRect;
}
</script>