简体   繁体   English

d3.js window.open对我不起作用

[英]d3.js window.open doesn't work for me

I need to open the urls in a new tab or window. 我需要在新标签或窗口中打开网址。 Right now they are opening in the same page. 现在,它们在同一页面中打开。

var data = [
{title: "1", url: "gallery1.html"},
{title: "2", url: "gallery2.html"},
{title: "3", url: "gallery3.html"},
];



    .attr("xlink:href", function(d) { return d.example.url; })
    .attr("xlink:title", function(d) { return d.example.title; });

svg.selectAll('.add-url-node')
   .on('click',function(d){
      location.href = 'target.url.com';

   });

I tried: 我试过了:

<script>    
window.open(url);      
</script>  

and

window.open(link, "_blank")

and

data.forEach(function(elem){
window.open(elem.url,"_blank");
});

and

.attr("xlink:target", "_blank") 

I have no programming experience, I would really appreciate to see an example using the code above. 我没有编程经验,我非常希望看到使用上述代码的示例。 many thanks. 非常感谢。

The problem seems to be the use of location.href . 问题似乎是使用location.href If you want the link to open in a new tab or window, and not in the same tab (page), you need to use window.open() : 如果要在新标签页或窗口中而不是在同一标签页中打开链接,则需要使用window.open()

selection.on('click',function(){
  window.open(url)
});

Note: You cannot force a link to open in a new tab instead of a new window. 注意:您不能强制链接在新选项卡而不是新窗口中打开。 It's a user preference . 这是用户的偏爱

Check this fiddle, I created 3 rectangles, and binded data to them with some links. 检查这个小提琴,我创建了3个矩形,并通过一些链接将数据绑定到了它们。 Click on each rectangle to open the link: https://jsfiddle.net/gerardofurtado/9s7wpb20/1/ 单击每个矩形以打开链接: https : //jsfiddle.net/gerardofurtado/9s7wpb20/1/

.attr("target","_blank", function(d) { return d.example.url; })

This is what I needed, 这就是我所需要的
Thanks everybody anyway 还是谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM