我有多个按钮,使用相同的tippy函数来创建动态模板。我需要从单击的按钮中获取data-value="“,并将其传递给tippyjs。
按钮
<div class="d-none d-lg-flex show-context-menu" data-value="song450">Tippy 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`);
},
});现在我有$(this)来获取按钮。我尝试了许多方法,但都不能传递数据id。
发布于 2020-09-11 14:31:26
试着这样做,希望能对你有所帮助。
$(".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>
https://stackoverflow.com/questions/63831499
复制相似问题