【发布时间】: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