【发布时间】:2015-05-02 17:13:00
【问题描述】:
我遇到了在 html 代码中操作 SVG 文件(对象)的挑战。 Snap、Raphael 中有解决方案,但我需要直接通过 JavaScript 或 JQuery 来完成。这就是我目前所拥有的:
JS:
<object id="testSVG" data="image_library/grandstaff_drawing_only.svg"
type="image/svg+xml" height=100% width=100%">
<img src="image_library/alto-clef.png" />
</object>
<script>
window.onload=function() {
// Get the Object by ID
var a = document.getElementById("testSVG");
// Get the SVG document inside the Object tag
var svgDoc = a.contentDocument;
// Get one of the SVG items by ID;
var svgItem = svgDoc.getElementById("path3380");
// Set the colour to something else
//svgItem.setAttribute("stroke", "red");
svgItem.style.stroke = "#ff0000";
};
</script>
jQuery:
<object id="testSVG" data="image_library/grandstaff_drawing_only.svg"
type="image/svg+xml" height=100% width=100%">
<img src="image_library/alto-clef.png" />
</object>
<script>
window.onload=function() {
var svgDoc = $(“#testSVG”)[0].contentDocument;
$(“#path3380”, svgDoc).css(“stroke”, “red”);
};
</script>
谢谢!!!
【问题讨论】:
-
你有什么问题?
标签: javascript jquery html svg