简体   繁体   English

Tippy.js是否不显示data属性的内容?

[英]Tippy.js not displaying content from the data attribute?

I've installed tippy.js to handle my tooltips, however I am struggling to get the tooltips to display the content set from the data attribute. 我已经安装了tippy.js来处理我的工具提示,但是我在努力获取工具提示以显示来自data属性的内容集。 My default tooltip is working fine, however when I initialise by a class, to be able to add a different style to the tooltip, it doesn't get the content from the tooltip. 我的默认工具提示工作正常,但是当我按类初始化时,为了能够在工具提示中添加其他样式,它不会从工具提示中获取内容。

tippy.setDefaults({
    animation: 'scale',
    animateFill: false,
    maxWidth: 240,
    duration: 0,
    arrow: false,
})

tippy('.js-tippy-reviews', {
    theme: 'reviews',
    animation: 'scale',
    animateFill: false,
    maxWidth: 240,
    duration: 0,
    arrow: false,
})

If I add the content method to tippy it will display, however since the content of the tooltip is dynamic, i need to set it on the data attribute. 如果将content方法添加到tippy,它将显示,但是由于工具提示的内容是动态的,因此需要在data属性上进行设置。 I am unsure how to pass the data attribute from the element into tippy. 我不确定如何将data属性从元素传递给tippy。

content: this.dataset.tippy

Any ideas what I'm doing wrong? 有什么想法我做错了吗?

HTML: HTML:

<div class="js-tippy-reviews reviews-notification" data-tippy="2 Pending Reviews"></div>

You could add the onShow() function and read it from the instance and set the content from the reference dataset. 您可以添加onShow()函数并从实例中读取它,并从参考数据集中设置内容。

 tippy.setDefaults({ animation: 'scale', animateFill: false, maxWidth: 240, duration: 0, arrow: false, }); tippy('.js-tippy-reviews', { theme: 'reviews', animation: 'scale', animateFill: false, maxWidth: 240, duration: 0, arrow: false, onShow(instance) { instance.popper.hidden = instance.reference.dataset.tippy ? false : true; instance.setContent(instance.reference.dataset.tippy); } }); $(document).ready(function(){ $("#btn-change-data").click(function() { $(".js-tippy-reviews").attr("data-tippy",$("#test-input").val()); }) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://unpkg.com/popper.js@1"></script> <script src="https://unpkg.com/tippy.js@4"></script> <div class="js-tippy-reviews reviews-notification" data-tippy="test">CONTENT</div> <input type="text" id="test-input" /> <button id="btn-change-data">Change data-tippy</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM