首页 > 解决方案 > 如何让我的对象重定向整个页面而不仅仅是它本身

问题描述

我的标签中有一个 SVG。当我点击它时,我想重定向页面。

<object class="SVGClass centered" data="/Data/Blue_Force.svg" type="image/svg+xml" id="BF1SVG"style="margin:20px"></object>
<script>
var SVG1 = document.getElementById("BF1SVG");
var SVGElements1;
var SVGLoaded1 = false;
    if(!SVGLoaded){
        SVG1.addEventListener('load',function(){ 
            SVGLoaded1 = true;
            SVGElements1 = SVG1.contentDocument;

      SVGElements1.getElementById("svgCC").setAttributeNS(null, "onclick", "window.location.href='http://686amppi5.campus.nist.gov/cryoConceptsStateViewer.html'");
});
</script>

svgCC 是外部 SVG 的 id。当我点击它时,对象本身变成了新页面,就像一个嵌入式窗口,而不是重定向浏览器。

我也试过:

document.getElementById("BF1SVG").setAttribute("onclick", "window.location.href='http://686amppi5.campus.nist.gov/cryoConceptsStateViewer.html'");

但这无济于事。我的 svg 不可点击。

标签: javascriptobjectdom

解决方案


You should not set the onclick attrbute that way... try this instead.

document.getElementById("BF1SVG").addEventListener(
    'click', 
    () => window.location.assign('http://686amppi5.campus.nist.gov/cryoConceptsStateViewer.html');
);

推荐阅读