我需要动态插入 SVG ,但是它应该是 INTERACTIVE ...
我在做正确的事吗?
我遇到的问题是SVG没有出现在页面上,如果我将其用作<image>
,它就不会交互。
HTML5代码
<!-- PLANTA -->
<main class="container-fluid d-flex align-items-center justify-content-center h-100">
<!-- SVG -->
<div id="info-box"></div>
<svg id="....." xmlns:svg="http://www.w3.org/2000/svg" width="500" height="500">
<object data="src/data/shoppings/plants/via-parque/via_parque_P1_20160909.svg" type="image/svg+xml" src="src/data/shoppings/plants/via-parque/via_parque_P1_20160909.png" > </object>
</svg>
</main>
JS代码
<script>
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON( "src/data/shoppings/lib/shoppings.json", function(data) {
var tst = document.getElementById("plant-src-svg")
var rect = document.createElementNS(tst,'object');
rect.setAttribute('type', "image/svg+xml");
rect.setAttribute('data', "src/data/shoppings/plants/via-parque/via_parque_P1_20160909.svg");
tst.appendChild(rect);
console.log(tst)
})
.always(function() {
console.log( "complete" );
})
.done(function() {
console.log( "second success" );
})
.fail(function() {
console.log( "error" );
});
</script>
答案 0 :(得分:0)
<object>
不应位于<svg>
内。只需将对象附加为父div的子代即可。
<!-- PLANTA -->
<main id="main" class="container-fluid d-flex align-items-center justify-content-center h-100">
<div id="info-box"></div>
<!-- object will be inserted here -->
</main>
JS:
var main = document.getElementById("main")
var object = document.createElement('object');
object.setAttribute('type', "image/svg+xml");
object.setAttribute("width", "500");
object.setAttribute("height", "500");
object.setAttribute('data', "src/data/shoppings/plants/via-parque/via_parque_P1_20160909.svg");
main.appendChild(object);