我的标签中有一个 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/