【问题标题】:Dynamic popover/tooltip via Bootstrap with flask & python带有烧瓶和python的Bootstrap中的动态弹出框/工具提示
【发布时间】:2025-12-20 19:15:06
【问题描述】:

我正在尝试使用 bootstrap (3.1)、flask 和 python 为网站创建动态生成的工具提示/弹出内容。

我在表格中有超链接项目。当我将鼠标悬停在这些超链接上时,我希望出现一个工具提示;当用户单击超链接时,我希望他们转到单个项目页面。代码(为简洁起见):

<table>
  <thead>
    <tr>
      <th>Item</th>
      <th>Quantity</th>
      <th>Color</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{% include 'itemlink.html' %}</td>
      <td>12</td>
      <td>red</td>
  </tbody>
</table>

然后在 itemlink.html 中:

 <a href="{{linkitem.get_url()}}" class="itemlink" data-item-id="{{linkitem.id}}">
   <img src="{{linkitem.image}}" alt="Item {{linkitem.id}}" class="smallicon" />
     {{linkitem|safe}}
 </a>

下一步:弹出框/工具提示框中我想要的内容:

<div id="item_{{boxitem.id}}" class="itembox">
    <div class="itemimage">
      <img src="{{boxitem.image}}" alt="{{boxitem.name}}" title="{{boxitem.get_title()}}" />
   </div>
   <div class="iteminfo">
     <div class="itemtitle">
       {{boxitem.get_title()}}
     </div>
     <div class="itemdesc">
        {% autoescape off %}{{boxitem.get_desc()}}{% endautoescape %}
      </div>
     </div>  
    </div>

我把它放在我页面上的一个脚本中:

<script>
  $(document).ready(function(){
   $('#item_{{item.id}}').popover({
     html: true,
     placement: 'right',
     trigger: 'hover',
     selector: '[rel="popover"]'
   });
  });
 </script>

我想知道这部分脚本是否可行?

  $('#item_{{item.id}}').popover({

编辑添加:(不是,它引发了服务器错误)
再次编辑:它必须是

$('.itembox#item_'+$(this).data(itemID')).popover


我应该在什么元素上添加 rel=popover?

【问题讨论】:

  • 它抛出了什么服务器错误?你能显示烧瓶服务器跟踪吗?

标签: javascript jquery python twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

我会给每个项目一个静态类,比如工具提示,或者给表格一个 id,并在表格中的元素上使用 jQuery 选择器来进行悬停事件。 $(document).ready(function(){ $('td.tooltip').popover({ html: true, placement: 'right', trigger: 'hover', selector: '[rel="popover"]' }); });

【讨论】:

  • 我试过这个——但是当我将鼠标悬停在任何单个元素上时,它会显示所有的弹出框。我认为它在正确的轨道上。 ^_^