【问题标题】:Adding an href link within a Bootstrap popover在 Bootstrap 弹出窗口中添加 href 链接
【发布时间】:2020-12-15 09:14:39
【问题描述】:

我使用过 Bootstrap 弹出框,在这个弹出框中,我想添加一个 href="somelink"。但这似乎不可能与当前的代码情况有关:

 <a 
          tabindex="0" 
          class="btn btn-link" 
          role="button" 
          data-toggle="popover" 
          data-trigger="focus" 
          title="Reaction Time (s)" 
          data-content="Enter the time it takes for a driver to react.
          Reaction times vary with age, distractions, alcohol or drug usage etc.
          The unit is seconds.">
              <i class="far fa-question-circle fa-2x"></i>
            </a>

我需要做什么才能在弹出框文本中添加一个 href?

【问题讨论】:

    标签: javascript html jquery twitter-bootstrap popover


    【解决方案1】:

    您需要在初始化弹出框时传递html:true

    演示代码

    $(function () {
    $("[data-toggle=popover]").popover({html:true})//add html as true to use html 
    });
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <a tabindex="0" class="btn btn-link" role="button" data-toggle="popover" data-trigger="focus" title="Reaction Time (s)" data-content="Enter the time it takes for a driver to react.
              Reaction times vary with age, distractions, alcohol or drug usage etc.
              The unit is seconds <a href='yoururl'>something</a>.">
      <i class="far fa-question-circle fa-2x">Click</i>
    </a>

    请查看this 了解更多信息。

    【讨论】:

    • 谢谢。这看起来会奏效。我会试一试。
    【解决方案2】:

    尝试将选项html 设置为truesanitize 设置为false

    https://getbootstrap.com/docs/4.3/getting-started/javascript/#sanitizer

    $('[data-toggle="popover"]').popover({
      sanitize: false
    })
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    
    <a tabindex="0" class="btn btn-link" role="button" data-toggle="popover" data-html="true" data-trigger="focus" title="Reaction Time (s)" data-content="Enter the time it takes for a driver to react.
              Reaction times vary with age, distractions, alcohol or drug usage etc.
              The unit is seconds. <a href='https://stackoverflow.com/'>Go</a>">
      <i class="far fa-question-circle fa-2x"></i>?
    </a>

    【讨论】:

      【解决方案3】:

      你需要这个吗?

      $("#testpopover").popover({
       html: true,
       title:"React time(s)",
       content: "Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum."+
       "<a class='btn btn-default'>HTML in popover</a>"
      })
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <!-- Latest compiled and minified CSS -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
      
      <!-- Optional theme -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
      
      <!-- Latest compiled and minified JavaScript -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
      
      
      
      <div style="width:100%; height:500px; margin-top:15%">
      <a type="button" class="btn btn-default" id="testpopover">popover</a>
      </div>

      【讨论】:

      • 我会试一试,看看它是否有效。我曾尝试使用 html: true,但我仍然无法获取锚标记,因为整个内容都锚定在弹出框的图标上。但我会看看这是否有效。
      猜你喜欢
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多