javascript - 在页面加载 5 秒后尝试显示tippy.js 时,instance.show 不是函数错误
问题描述
我正在使用tippyJS在悬停时显示一个文本弹出窗口,但我也想在页面加载5秒后显示这个弹出窗口,所以我这样做了:
const instance = tippy('.tooltippy', {
trigger: 'mouseenter',
allowHTML: true,
placement: 'left',
animation: 'scale-subtle',
interactive: true,
content: function (reference) {
return reference.querySelector('.tooltipcontent');
}
});
setTimeout(function() {
instance.show();
}, 5000);
但 5 秒后我得到:
Uncaught TypeError: instance.show is not a function
虽然我的tippyjs 工作正常。
我的html:
<span class="whatsappicon tooltippy" title="Chat met ons!">
<span class="whaplabel tooltipcontent">Stel je vraag!</span>
<a href="url" target="_blank">
<img src="images/custom/whatsapp_online.png" alt="Open Whatsapp">
</a>
</span>
5 秒后我能做些什么来打开这个小提示?
解决方案
更新了我的问题,因为我没有看到您将它直接分配给 HTMLCollection。Tippy需要绑定NodeList或者Element类型,所以我们需要先获取元素并赋值给它。
这个例子应该工作:
const tooltippy = document.getElementById('tooltippy')
const instance = tippy(tooltippy, {
trigger: 'mouseenter',
allowHTML: true,
placement: 'left',
animation: 'scale-subtle',
interactive: true,
content: function (reference) {
return reference.querySelector('.tooltipcontent');
}
});
setTimeout(function() {
instance.show();
}, 5000);
<script src="https://unpkg.com/@popperjs/core@2/dist/umd/popper.min.js"></script>
<script src="https://unpkg.com/tippy.js@6/dist/tippy-bundle.umd.js"></script>
<span class="whatsappicon" id="tooltippy" title="Chat met ons!">
<span class="whaplabel tooltipcontent">Stel je vraag!</span>
<a href="url" target="_blank">
<img src="images/custom/whatsapp_online.png" alt="Open Whatsapp">
</a>
</span>
推荐阅读
- html - 为列表应用平滑渐变颜色
- javascript - 如何使用 Vuex 向数组对象添加新值?
- java - 计算器的多个 onClick 功能?
- c# - 处理“System.Net.Websocket-Exception:远程方在未完成关闭握手的情况下关闭连接。”?
- c# - UML 类图。在一个类下声明一个类
- node.js - electronjs NODE_MODULE_VERSION 错误和重建命令无法解决问题
- php - 访问php文件返回404代码但在其他文件上成功
- google-apps-script - 警报显示许多不必要的信息
- php - 我有这个雄辩的文件,但我收到未定义的变量
- c# - 如何使用基于构建配置的模式包含文件?