我在内容脚本的帮助下添加了一个弹出式Div。我添加了一个关闭该DIV的按钮,为此我为我的函数 popClose()调用了 onClick 事件,但它给了函数undefined。我该如何实现?
我的内容脚本代码如下:
var currentURL = document.location.href;
var body = document.getElementsByTagName("body")[0];
body.setAttribute("style", "background-color:red");
//get Top BlueBar Reference
var blueBar = document.getElementById("blueBar");
var blueBarHtml = blueBar.innerHTML;
//popUp Div for Options
var popUpDiv = "<div onclick='closepop();' class='popUpDiv'><div class=close>X</div></div>";
blueBar.innerHTML = popUpDiv+blueBarHtml;
//get page Nav ID
var tinyMan = chrome.extension.getURL("tinyman.png");
var pageNav = document.getElementById("pageNav");
var red = chrome.extension.getURL("red.png");
var yellow = chrome.extension.getURL("yellow.png");
var green = chrome.extension.getURL("green.png");
var blue = chrome.extension.getURL("blue.png");
var pageNavHtml = pageNav.innerHTML;
var liHTML = '<li id="Extra" class="navItem topNavLink middleLink">';
liHTML+='<a><img src="'+tinyMan+'" border="0" style="margin-bottom: 4px; display: inline-block;">';
liHTML+="<a>"
liHTML+='<div class="menuPulldown" style="display:block;"></div>'
liHTML+='</a>';
liHTML+='</li>';
//set new HTML
pageNav.innerHTML = liHTML+pageNavHtml;
function closepop()
{
alert("jj");
}
答案 0 :(得分:1)
好吧,我刚刚在DOM的头部添加了我的函数内容,然后调用它。
var head = document.getElementsByTagName("head")[0];
fn = "function closepop(){alert('jj');}";
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = fn;
head.appendChild(script);
有没有更干净的方法呢?