0

我在内容脚本的帮助下添加了一个弹出 Div。我添加了一个按钮来关闭该 DIV,为此我为我的函数popClose()调用了onClick事件,但它给出了未定义的函数。我该如何实现?

我的内容脚本代码如下:

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");
}
4

1 回答 1

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);

有没有更清洁的方法呢?

于 2013-07-29T20:26:04.753 回答