javascript - 首次点击动态生成的内容时弹出窗口不显示

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

我正在尝试为我的网页获取弹出窗口。第一次点击时它对我不起作用,但此后效果很好。我意识到它会在第一次单击时实例化,因此不会显示。鉴于我使用的是在运行时生成的 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();
            }
        });
    });

最佳答案

您可以向弹出窗口添加两个点击处理程序。第一个使用“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);

});

关于javascript - 首次点击动态生成的内容时弹出窗口不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31551352/

相关文章:

javascript - TypeScript 中函数或变量的契约是什么

javascript - 如何在不触发最后一个的情况下调用更多 href 的 onclick - 嵌入式 javascript 模板

javascript - Raphael js,如何获取圆心的x、y位置?

jquery - 将内容放在数据元素的末尾 - 如何使用数据元素进行选择

ruby-on-rails - 由于 Mac 上的编译器问题,无法安装 therubyracer gem?

javascript - 滚动时将元素悬停在鼠标光标下方

javascript - 在 Javascript 中淡出

jquery - 使用 css 设置大小

ruby-on-rails - Uncaught Error : [$injector:unpr] Unknown provider: eProvider <- e while deploying to heroku

ruby-on-rails - ActiveRecord::Base:Class 的未定义方法 `migration_error='