javascript - 如何让我的对象重定向整个页面而不仅仅是它本身
问题描述
我的标签中有一个 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 不可点击。
解决方案
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');
);
推荐阅读
- macos - 使用 homebrew 安装旧的 pypy3 版本
- angular - Angular post方法类型'Observable
' 不可分配给类型 'Observable'。类型“HttpEvent”不可分配给类型“TestUser” - firebase - Flutter 没有为“UserCredential”类型定义 getter 'uid'
- python - 使用 .loc 基于时间序列删除行
- swift - PassthroughSubject 完成完成未调用
- r - 使用自定义投影仪主题在 rmarkdown::beamer_presentation 中创建从徽标到目录幻灯片的超链接
- php - 获取第一个分隔符和最后一个点之间的字符串
- android - 当我的手指触摸屏幕时,自动点击停止
- vue.js - nuxt 配置,$route 未定义?
- r - 在R中使用summarySE函数时,如何抑制仅具有一个观察值的分组的NaN值的警告消息?