【问题标题】:Bootstrap popover is not a functionBootstrap popover 不是一个函数
【发布时间】:2017-03-10 05:27:46
【问题描述】:

我目前正在尝试开发一个 Chrome 插件,可以在每个访问的网页中发现不同名称的出现。

当插件找到一个名字时,它基本上会在它旁边添加一个图标。点击后,应该会出现一个引导弹出框,其中包含有关此人的更多信息。

当我尝试运行我的代码时,我收到了这个错误:

VM887:1 Uncaught TypeError: $(...).popover is not a function(...)

这是我的代码:

$('head').append('<script async src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>\
<script async src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>');

$(document).ready(function(){
  var name = "Nicolas";
  // console.log('Document is ready.. scanning for politicians...');
  //var hashmap = db_reader.getHashMap();

  var counter = {i: 0}; //Occurences. Singleton to be passed by reference and not by value.
  $('p').each(function(index) {
    addImage(this, counter);
  });

  $('li').each(function(index) {
    addImage(this, counter)
  });

  $('head').append(
    "<script>$(function(){$('[data-toggle=\"popover\"]').popover();});\
</script>"
  );

});

function addImage(context, counter) {
  var body = $(context).text();
  var word;
  var reg = /[A-Z]+[a-z]*/gm;
  while(word = reg.exec(body)){
    // console.log("while");
    if(word == name){
      // console.log(word);
      var image = '<a href="#" data-toggle="popover" title="Popover Header" data-content="A wonderful content" id="popover'+counter.i+'" class="politicianFind">\
<img alt="name" src="https://s15.postimg.org/pei4ci3fv/fdp.png">\
</a>';
      // console.log("Replacing HTML");
      $(context).html(body.replace(word, word + " " + image));
      counter.i++;
    }
  }
}

我已经搜索了很多,但找不到任何解决此问题的方法。

亲切的问候,

弗洛里安

【问题讨论】:

  • 您的项目中是否包含了 bootstrap.js?
  • 谢谢!确实,它解决了我的问题。我只是忘了将 boostrap、jQuery 依赖项添加到清单文件中 :)
  • 当控制台中的某些内容“未定义”或“不是函数”时没有问题,这是首先要排除故障的地方。

标签: javascript jquery html twitter-bootstrap


【解决方案1】:

我认为这是因为当您调用方法popover 时,引导脚本尚未加载。

你有两个选择:

  1. 调用$(window).load-http://output.jsbin.com/qoteqe上的方法
  2. 在 DOM 中包含 bootstrap 和 jQuery 引用,并且不在脚本中加载它们:http://output.jsbin.com/tamoda

顺便说一下为什么要添加另一个 jQuery 引用?如果您使用$('head').append 显然您的页面中已经有jQuery。不是吗?

【讨论】:

  • 您好,感谢您的回答。我忘记将依赖项添加到清单中...我通过将 jQuery 和 Bootstrap 依赖项添加到清单文件中解决了这个问题。谢谢你的回答。
猜你喜欢
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 2018-06-16
  • 2014-11-03
  • 1970-01-01
  • 2016-11-02
相关资源
最近更新 更多