简体   繁体   English

Tippyjs - 如何将单击按钮上的数据值传递给模板

[英]Tippyjs - How to pass data-value on clicked button to the template

I have multiple buttons using the same tippy function that creates a dynamic template.我有多个按钮使用相同的tippy 函数来创建动态模板。 I need to take the data-value="" from the clicked button and pass it to the tippyjs.我需要从单击的按钮中获取 data-value="" 并将其传递给tippyjs。

The button按钮

<div class="d-none d-lg-flex show-context-menu" data-value="song450">

Tippy js尖尖的 js

 tippy(".show-context-menu", {
 
  
  trigger: "click",
  placement: "bottom-start",
  allowHTML: true,
  interactive: true,
  hideOnClick: true,
  inertia: true,
  onShow(instance) {
    
    if ($("#parent-row-" + $(this).data("value")).length) {
      $("body").addClass("no-scroll");
      var e = $.parseJSON($("#parent-row-" + $(this).data("value")).html());
      console.log("#parent-row-" + $(this).data("value"));
    } else e = CurrentSongInfo,
   
    instance.setContent(` Dynamic template is here`);
  },
});

Right now I have the $(this) for getting the button.现在我有 $(this) 来获取按钮。 I have tried a number of ways but nothing is working to pass the data id我尝试了多种方法,但没有任何方法可以传递数据 ID

Try like this, hope it helps you.试试这个,希望对你有帮助。

 $(".show-context-menu").each(function() { tippy($(this)[0], { content: $(this).attr("data-value"), trigger: "click", placement: "bottom-start", allowHTML: true, interactive: true, hideOnClick: true, inertia: true, }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://unpkg.com/@popperjs/core@2"></script> <script src="https://unpkg.com/tippy.js@6"></script> <div class="show-context-menu" data-value="song450">Button 1</div> <div class="show-context-menu" data-value="song550">Button 2</div> <div class="show-context-menu" data-value="song650">Button 3</div>

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

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