gpt4 book ai didi

javascript - 为什么类的每个实例都会触发多次点击?

转载 作者:行者123 更新时间:2023-12-02 23:07:07 25 4
gpt4 key购买 nike

我在页面上有三个工具提示按钮。我可以通过单击按钮外部的任何位置来关闭任何打开的工具提示。这就是我遇到的情况:

在下面的代码中,当我点击页面上的任何位置时,这部分代码的处理程序就会被激活 $(document).on('click', (event) => this.closeOnOutsideClick(事件));我可以在检查器中看到,该函数 closeOnOutsideClick 被触发了三次 - 它对页面上存在的每个工具提示按钮进行了三次检查。我无法弄清楚是什么机制造成了这种情况,以及为什么检查 if (!$(event.target).closest(this.$elem)) 不只执行一次?我的代码可以在这里和下面找到:https://jsfiddle.net/bakrall/786cz40L/

这是更复杂代码的简化版本,只是为了说明我的问题:

const selectors = {
tooltip: '.tooltip-container',
tooltipButton: '.tooltip-button',
tooltipMessage: '.tooltip-message'
}

class Tooltip {
constructor(tooltip) {
this.$elem = $(tooltip);
this.$tooltipButton = this.$elem.find(selectors.tooltipButton);
this.$tooltipMessage = this.$elem.find(selectors.tooltipMessage);
this.$tooltipMessageText = this.$tooltipButton.attr('data-tooltip-content');

this.bindUiEvents();
}

bindUiEvents() {
$(document).on('click', (event) => this.closeOnOutsideClick(event));
this.$tooltipButton.on('click', () => this.showTooltipMessage());
this.$tooltipButton.on('blur', () => this.hideTooltip());
}

showTooltipMessage() {
this.$tooltipMessage
.text(this.$tooltipMessageText)
.addClass('shown-message');
}

hideTooltip() {
this.$tooltipMessage
.text('')
.removeClass('shown-message');
}

closeOnOutsideClick(event) {
if (!$(event.target).closest(this.$elem)) {
this.hideTooltip();
}
}
}


//class in another file
const tooltip = $('.tooltip-container');

tooltip.each(function(index, item) {
new Tooltip(item);
})
.input-wrapper {
margin-bottom: 2em;
}

.tooltip-container {
position: relative;
display: inline-block;
}

.tooltip-message {
display: none;
position: absolute;
left: 100%;
top: 0;
width: 10em;
padding: 0.5rem;
background: #000;
color: #fff;
}

.tooltip-message.shown-message {
display: inline-block;
}

button {
width: 1.2em;
height: 1.2em;
border-radius: 50%;
border: 0;
background: #000;
font-family: serif;
font-weight: bold;
color: #fff;
}

button:focus {
outline: none;
box-shadow: 0 0 0 0.25rem skyBlue;
}

input {
display: block;
}
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title> </title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="tooltip.css">
</head>
<body>
<div class="input-wrapper">
<label for="name">
What's your name?
<span class="tooltip-container">
<button class="tooltip-button" type="button" aria-label="more info"
data-tooltip-content="This clarifies whatever needs clarifying">i</button>
<span class="tooltip-message" role="status"></span>
</span>
</label>
<input id="name" type="text"/>
</div>
<div class="input-wrapper">
<label for="age">
What's your age?
<span class="tooltip-container">
<button class="tooltip-button" type="button" aria-label="more info"
data-tooltip-content="This is to know how old you are">i</button>
<span class="tooltip-message" role="status"></span>
</span>
</label>
<input id="age" type="text"/>
</div>
<div class="input-wrapper">
<label for="nationality">
What's your nationality
<span class="tooltip-container">
<button class="tooltip-button" type="button" aria-label="more info"
data-tooltip-content="What country are you from?">i</button>
<span class="tooltip-message" role="status"></span>
</span>
</label>
<input id="nationality" type="text"/>
</div>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous">
</script>
<script src="tooltip.js" async defer></script>
</body>
</html>

最佳答案

tooltip.each(function(index, item) {
new Tooltip(item);
})

由于您实例化了 3 个工具提示,因此每次都会将一个单独的事件监听器绑定(bind)到文档。每次点击都会触发每个监听器。但是,每个监听器都有不同的 this,这允许每个监听器判断其工具提示是否被单击,如果没有,则隐藏它。

如果您想要一个监听器,您可以存储所有工具提示的列表,并让事件监听器遍历工具提示列表,关闭所有未单击的工具提示。

关于javascript - 为什么类的每个实例都会触发多次点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57535356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com