var myContainer: any = null;<div id="container" ref={container => myContainer = container}> <p id="child" > test</p></div>现在我想知道 event.target 是否在 ref 内。document.addEventListener("click", function (event) { var target = event.target; if(target is inside myContainer){ console.log("target is inside my container"); } });
3 回答
长风秋雁
TA贡献1757条经验 获得超7个赞
您可以使用contains. 尝试这样的事情
document.addEventListener("click", function (event) {
var target = event.target;
if(this.myContainer && this.myContainer.contains(target)){
console.log("target is inside my container");
}
});
添加回答
举报
0/150
提交
取消