javascript - 如何使我的对象重定向整个页面而不仅仅是它本身

标签 javascript object dom

我的标签中有一个 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 不可点击。

最佳答案

你不应该那样设置 onclick 属性......试试这个。

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

关于javascript - 如何使我的对象重定向整个页面而不仅仅是它本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57436594/

相关文章:

javascript - Div 是空的,即使它有一个 child

javascript - 使用 php 从 ajax 将值选择到文本框中

javascript - 图像渲染值有时会更新,有时不会

javascript - 如何将对象插入到对象内部的数组中?

java - 如果有的话,在 java 中声明给定类的空对象字段时会发生什么类特定操作?

javascript - 使用 jQuery 向表动态添加行和列

javascript - postman 中的表单数据将空对象发送到node.js

javascript - bxSlider 不自动工作

javascript - 变量未在对象中定义

css - 隐藏以特定文本作为内容的 div