【问题标题】:Popover not showing on first click for dynamically generated content弹出框在第一次点击动态生成的内容时不显示
【发布时间】:2015-07-21 23:26:06
【问题描述】:

我正在尝试为我的网页创建一个弹出框。第一次单击时它对我不起作用,但此后可以正常工作。我意识到它在第一次点击时被实例化,因此不会出现。有没有更好的方法来实例化它,因为我使用的是在运行时生成的 id。我看过类似的问题,但它们似乎对我不起作用。这是我写的代码,

    <table id="userInfo">
  <thead>
       <tr>
        <th>UserGroup</th>
      </tr>
  </thead>

  <tbody>
  <% @userdetails.each do |user| %>
      <tr>
        <td>
          <a class='user_show_more' data-placement='left' id='<%= user %>'>[+]</a>
              <!-- popover table starts here -->
              <div id="<%= user %>_popover" style="display: none">
                <table>
                  <thead>
                  <tr>
                    <th>UserNames</th>
                  </tr>
                  </thead>
                  <tbody>
                  <tr>
                    <td>
                      <%= user['name'] %>
                    </td>
                  </tr>
                  </tbody>
                </table>
              </div>
              <!-- popover ends here -->
          <% user['group'] %>
        </td>
      </tr>
  <% end %>
  </tbody>
</table>

我的 javascript 代码看起来像这样,

    $('.user_show_more').on('click', function (e) {
        var $this = $(this)
        var popover_id = '#'+$this.attr("id");
        $(popover_id).popover({
            html : true,
            content: function() {
                var popover = '#'+$this.attr("id")+'_popover';
                return $(popover).html();
            }
        });
    });

【问题讨论】:

  • 我相信你在初始化popover之后仍然需要调用show。添加“$(popover_id).popover('show');”到您的点击处理程序的末尾。

标签: javascript jquery ruby-on-rails twitter-bootstrap popover


【解决方案1】:

您可以向弹出框添加两个点击处理程序。第一个使用“one”,第二个使用“on”。

https://jsbin.com/kegovebufu/1/edit?html,js,console,output

HTML

  <button type="button"
          class="btn btn-lg btn-danger"
          data-toggle="popover" 
          id="Temp"
          >Stuff</button> 


  <script type="text/html" id="Temp_popover">
   <div>  
      <table>
        <thead>
          <tr>
           <th>UserNames</th> 
          </tr>
        </thead>
         <tbody>
          <tr>
           <td>blah</td>
          </tr>
         </tbody>
       </table>
    </div>      
  </script>

Javascript

    function PopoverClick()
{
  $(this).popover('toggle'); 
}




$('[data-toggle="popover"]').one('click',function(e){

  console.log('once');
  var _this = $(this);


  _this.popover(
  {
    html:true,
    content: function()
    {
      return $('#'+$(this).attr("id")+'_popover').html();         
    },
    trigger: 'manual',
    placement:'right'    
  }).popover('show');  

  _this.on('click',PopoverClick);

});

【讨论】:

  • 第一次点击有效,之后就无效了。
  • @imuqtadir 试试这个
  • @imuqtadir 您是否也在尝试优化页面加载或您的最终目标是什么?
猜你喜欢
  • 2013-02-27
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 2013-05-18
  • 1970-01-01
相关资源
最近更新 更多