【问题标题】:Add id to the default template of jquery popover将id添加到jquery popover的默认模板中
【发布时间】:2021-08-28 15:02:59
【问题描述】:

我想创建一个弹出框,但我也想给它一个 id。我试图通过设置这样的模板来实现这一点。但是当我尝试显示弹出框时,出现以下错误

$("#myElement").popover({
            trigger: "manual",
            placement: placement,
            html: true,
            template: `<div id="` + id + `" class="popover fade bs-popover-end" style="position: absolute; inset: 0px auto auto 0px; margin: 0px; transform: translate(788px, 615px);"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>`,
            customClass: customClass,
            content: content,
        })

错误:

Uncaught TypeError: Cannot read property 'remove' of null
    at Qe.getTipElement (popover.js:100)
    at Qe.show (tooltip.js:241)
    at HTMLImageElement.<anonymous> (popover.js:155)
    at Function.each (jquery.min.js:2)
    at S.fn.init.each (jquery.min.js:2)
    at S.fn.init.jQueryInterface [as popover] (popover.js:147)
    at PopoverList.Show (main.js:74)

【问题讨论】:

    标签: javascript jquery popover


    【解决方案1】:

    这行得通。
    您在模板中做了一个变量 id。
    id 是从哪里来的? 例如,我给了 id 值 1。

    $(function() {
      $('#myelement').popover({
        trigger: 'manuel',
        placement: 'bottom',
        content: "And here's some amazing content. It's very engaging. Right?",
        title: "Popover title",
        html: true,
        template: '<div class="popover" role="tooltip"><div class="arrow"></div><h3 class="popover-header"></h3><div class="popover-body"></div></div>'
      })
      $('#myelement').on('shown.bs.popover', function() {
        console.log($(this).attr('aria-describedby'));
      })
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" />
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
    <button type="button" id="myelement" class="btn btn-lg btn-danger">Click to toggle popover</button>

    【讨论】:

    • 非常感谢。现在它正在工作。并且id是之前设置的。我没有发布那部分
    • 哦,其实没用。我的意思是我没有收到错误,但弹出框没有我设置的 id
    • 我更新了我的答案。请检查它是否适合您。 popover self 已经自动创建了一个 id。请参阅上面的 console.log 示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2019-03-14
    • 2021-03-19
    • 2017-02-09
    相关资源
    最近更新 更多