【问题标题】:HTML: nesting strings in strings in attributesHTML:在属性中的字符串中嵌套字符串
【发布时间】:2019-02-27 20:26:40
【问题描述】:

我的网站包含带有 HTML 代码的引导弹出框。这样的弹出框看起来像这样(而不是弹出 HTML,为了便于阅读,我放入了一个占位符):

<mark data-content="Fancy HTML here" data-html="true" data-toggle="popover">
    This mark has a popover
</mark>

data-content 中插入真实的 HTML 会导致正确转义引号的问题。我发现了这个问题,看起来完全一样: How do I encode html for Bootstrap popover and tooltip content

接受的答案建议将引号转义为&amp;quot; 这不起作用,转义的引号没有正确解析。我在这个小提琴中设置了一个例子:https://jsfiddle.net/z4t2sud3/3/

另一种选择是使用单引号' 表示弹出窗口中的字符串。这个选项也在小提琴中并且可以正常工作。

但是如果我想嵌套更深怎么办?例如,我插入到data-content 的 HTML 也可能包含弹出框。虽然这似乎是一个非常糟糕的设计理念,但让它发挥作用会很好。

这是双嵌套弹出框的小提琴:https://jsfiddle.net/cwz3sayj/3/

【问题讨论】:

    标签: javascript html bootstrap-4


    【解决方案1】:

    希望下面的 sn-p 看起来像您想要的那样,您可以在创建弹出框实例时传递一个选项,您可以在其中设置单击实例时将显示的内容。由于第一次运行脚本时 DOM 中不存在第一级,因此您必须在单击第一个弹出框后激活第二个弹出框。使用模板文字,您可以在编写的 html 代码中使用撇号或引号。

    $('#firstPopover')
        .popover({
            html: true,
            content: `<button id="secondPopover" class="btn">
                          Goto second level
                      </button>`
        })
        .on('shown.bs.popover', function() {
            $('#secondPopover').popover({
                html: true,
                content: `This is the last level`
            });
        });
    <br />
    <button id="firstPopover" data-html="true" class="btn">
      Goto first level
    </button>
    
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

    现在唯一的问题是第一层在第二层之前关闭。

    【讨论】:

    • 那太美了。以编程方式执行此操作很有意义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2021-11-01
    • 2019-04-19
    相关资源
    最近更新 更多