【问题标题】:How to differentiate popover by id or class如何通过 id 或 class 区分 popover
【发布时间】:2020-03-06 06:27:38
【问题描述】:

您好,我面临一个问题,我有两个弹出框,其中我正在传递 html

这是我的代码

<button class="count" data-toggle="popover">+click me</button>
<div  class="popover_content_wrapper" style="display: none">
  <p>Rahul</p>
</div>

<button class="count" data-toggle="popover">+click me</button>
<div  class="popover_content_wrapper" style="display: none">
  <p>Dravid</p>
</div>

在我的 Js 部分

  $(function() {
    $('[data-toggle="popover"]').popover({ html : true,
        content: function() {
            return $('.popover_content_wrapper').html();
        }
    })
  })

但如果我点击第一个按钮,它工作正常并在弹出窗口中显示“Rahul”

但如果我点击第二个按钮,它也会显示“Rahul”,但我希望它需要显示“Dravid”

【问题讨论】:

  • 试试:return $(this).find('.popover_content_wrapper').html();
  • 请尊重发帖顺序。接受的答案是上述评论的实现。我的解决方案要短得多

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

从 $(this) 导航

$(function() {
  $('[data-toggle="popover"]').popover({
    html: true,
    content: function() {
      return $(this).next().text(); // or .html();
    }
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>


<button class="count" data-toggle="popover">+click me</button>
<div class="popover_content_wrapper" style="display: none">
  <p>Rahul</p>
</div>

<button class="count" data-toggle="popover">+click me</button>
<div class="popover_content_wrapper" style="display: none">
  <p>Dravid</p>
</div>

【讨论】:

    【解决方案2】:

    只需使用$(this).next(),因为它是紧接着div 到点击button

    $(function(event) {
        $('[data-toggle="popover"]').popover({ html : true,
            content: function() {
                return $(this).next('.popover_content_wrapper').html();
                // or $(this).next().html()
            }
        })
      })
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    
    <button class="count" data-toggle="popover">+click me</button>
            <div  class="popover_content_wrapper" style="display: none">
                 <p>Rahul</p>
             </div>
    
    <button class="count" data-toggle="popover">+click me</button>
            <div  class="popover_content_wrapper" style="display: none">
                 <p>Dravid</p>
             </div>

    【讨论】:

    • @mplungjan 我没有复制你的代码。我只是在 js code snippet 内写,同时添加 bootstrap dependencies 。看来你和我几乎是同时发帖的。
    • 我没有指责你抄袭(我看你的依赖和我的不一样)
    猜你喜欢
    • 2014-01-09
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多