javascript - Tippy.JS“TypeError:instance.setContent 不是函数”
问题描述
当我尝试在 Tippy.JS 中设置工具提示的内容时,我收到错误消息TypeError: instance.setContent is not a function
。
这是我的代码:
var instance = tippy('#range', {
placement: 'bottom',
content: '',
});
$('#range').on('input', function () {
instance.setContent('Hello World!');
});
解决方案
尽管可能很奇怪,但是无论您使用什么类型的选择器,tippy 都会返回一个 ARRAY。那是因为tippy函数可以根据选择器返回多个项目。例如。
tippy("[notice='tippy']",{
theme:'light',
allowHTML:true,
placement: 'bottom',
content: contentElement.innerHTML,
animation:'scale-subtle',
trigger:'click',
interactive: true,
maxWidth: "none",
allowHTML:true,
onShow(instance) {
instance.setContent(contentElement.innerHTML);
this.windowHeight = window.innerHeight;
}
})
上面的代码选择了所有具有属性 notice 的元素,其值为 'tippy' 并返回一个 Tippy 实例数组
[{/*Tippy Object*/},{/*Tippy Object*/}] //Javascript Array of Objects
如果您确定只返回一个元素,我建议您使用 [0] 仅返回数组中的那个元素。
let oneTippy = tippy("#tippyElement",{
theme:'light',
allowHTML:true,
placement: 'bottom',
content: contentElement.innerHTML,
animation:'scale-subtle',
trigger:'click',
interactive: true,
maxWidth: "none",
allowHTML:true,
onShow(instance) {
instance.setContent(contentElement.innerHTML);
this.windowHeight = window.innerHeight;
}
})[0]
推荐阅读
- javascript - 自适应卡片在提交中输入值
- python - 二维数组中的偶数池化
- sqlite - Node-Red 无法打开 SQLite 数据库
- c# - 使用 Automapper 将 ExpandoObject 字符串属性转换为 DateTime
- python - InvalidArgumentError:矩阵大小不兼容:In[0]:[32,21],In[1]:[128,1]
- python - 在两个表之间绘制箭头(作为图像)(Python)
- typescript - 有人可以告诉我如何禁用由@typescript-eslint/ban-types 触发的所有内容吗?
- javascript - React 是否会阻止用户在登录后查找组件?
- oracle - Oracle LISTAGG 和 Coalesce
- javascript - 为什么传递给函数的对象可以被改变但不能在javascript中重新分配?