javascript - d3.js when click outside the elements, hide the element - Stack Overflow

I have some tootips in my page. There is one tooltip(div) contains a input box and a button. I want to

I have some tootips in my page. There is one tooltip(div) contains a input box and a button. I want to click outside the tooltip and then hide the tooltip. I tried this:

d3.select("body")
.on("click",function(){
    d3.select("#tooltip")
    .classed("hidden",true);
});

This works, but when I click the input box want to input some value, the tooltip still hide. How can I hide the tooltip only when I click outside the tooltip?

I also tried:

d3.select("body").filter().on("click",function(){})

But I don't how it works, I cannot select all elements except the tooltip(div).

I have some tootips in my page. There is one tooltip(div) contains a input box and a button. I want to click outside the tooltip and then hide the tooltip. I tried this:

d3.select("body")
.on("click",function(){
    d3.select("#tooltip")
    .classed("hidden",true);
});

This works, but when I click the input box want to input some value, the tooltip still hide. How can I hide the tooltip only when I click outside the tooltip?

I also tried:

d3.select("body").filter().on("click",function(){})

But I don't how it works, I cannot select all elements except the tooltip(div).

Share Improve this question asked Apr 6, 2016 at 4:37 YaweiYawei 931 silver badge8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Here is a solution:

var tooltip = d3.select("#tooltip");
var tooltipWithContent = d3.selectAll("#tooltip, #tooltip *");

function equalToEventTarget() {
    return this == d3.event.target;
}

d3.select("body").on("click",function(){
    var outside = tooltipWithContent.filter(equalToEventTarget).empty();
    if (outside) {
        tooltip.classed("hidden", true);
    }
});

JSFiddle: https://jsfiddle/LukaszWiktor/53yok58w/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742354551a4428177.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信