d3.js窗口.open没有'It’我不管用

d3.js window.open doesn't work for me

本文关键字:It 不管 窗口 js open 没有 d3      更新时间:2023-09-26

我需要在新的选项卡或窗口中打开URL。现在它们在同一页打开。

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';
   });

我试过了:

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

window.open(link, "_blank")

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

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

我没有编程经验,我真的很高兴看到一个使用上面代码的例子。非常感谢。

问题似乎是location.href的使用。如果您希望链接在新的选项卡或窗口中打开,而不是在同一选项卡(页面(中打开,则需要使用window.open():

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

注意:不能强制在新选项卡而不是新窗口中打开链接。这是用户偏好

检查这个小提琴,我创建了3个矩形,并用一些链接将数据绑定到它们上。单击每个矩形打开链接:https://jsfiddle.net/gerardofurtado/9s7wpb20/1/

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

这正是我所需要的,
无论如何都要感谢大家