Kiw*_*iwi 0 javascript api jquery last.fm
我正在尝试使用last.fm api进行chrome扩展,并且我已经成功地放下了我希望它显示的页面中的fol div.
<div id="showLastFMInfo" Title="More info about song" class="LastFMButtonDown">»</div>
Run Code Online (Sandbox Code Playgroud)
但当我点击它时,没有任何反应,这是JS使用的:
$(document).ready(function () {
// this get executed
$('#meta-frame').append('<div id="showLastFMInfo" Title="More info about song" class="LastFMButtonDown">»</div>');
// this won't work if I click on my button
$("#showLastFMInfo").click(function(){
console.log("Click");
});
});
Run Code Online (Sandbox Code Playgroud)
所以你可以看到第一行被执行但.click()没有反应.
这是我的manifest.json:
{
"content_scripts": [ {
"js": [ "jquery.js", "lastfm.api.cache.js","lastfm.api.md5.js", "lastfm.api.js","lastFMLink.js", "script.js"],
"css": [ "LastFMLink.css" ],
"run_at": "document_end"
} ],
"name": "Plug.Dj VS last.Fm",
"description": "Implement information about the artist",
"icons": { "16": "cookie.png", "48": "cookie.png", "128": "cookie.png" },
"version": "0.0.1",
"web_accessible_resources": [ "lastFMLink.js" ],
"manifest_version": 2
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我做错了什么?
你要附加它,所以它是动态的,你需要一个委托的事件处理程序:
$('#meta-frame').on('click', '#showLastFMInfo', function(){
console.log("Click");
});
Run Code Online (Sandbox Code Playgroud)
另一方面,在追加元素后附加事件处理程序也应该有用吗?