javascript - 添加按钮以关闭面板

标签 javascript html firefox firefox-addon firefox-addon-sdk

我想添加一个关闭按钮来关闭浏览器中显示的面板。为此,我有一个 javascript 文件,它启动 prompt.html 文件,即我的面板

var panel = require('sdk/panel').Panel({
    width  : 400,
    height : 400,
    contentURL : self.data.url('prompt.html')
});

然后在我的 prompt.html 中我有以下内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>MyPanel</title>
        <script type="text/javascript" src="util.js"></script>
        <script type="text/javascript">
    <body class="din">
        <h1>MyPanel</h1>
        <div id="div1"></div>
        <div id="div2"></div>
    </body>

该脚本包含向 div 添加节点并显示内容的代码。我的问题是:如何添加一个按钮来关闭面板?

最佳答案

  1. 向面板添加一个按钮。
  2. 单击时,self.port.emit 会显示一些消息,告诉您的 main.js 代码隐藏面板。
  3. 收到消息后,调用panel.hide()

工作示例

main.js

var self = require("sdk/self");

var panel = require('sdk/panel').Panel({
  width  : 400,
  height : 400,
  contentURL : self.data.url('prompt.html'),
  contentScriptFile: self.data.url('prompt.js')
});

// On "close" messages from the content script...
panel.port.on("close", function() {
  console.log("asked to close");
  // hide/close the panel.
  panel.hide();
});

// Initially show the panel for testing purposes.
panel.show();

提示.html

<!doctype html>
<html>
  <head>
    <title>MyPanel</title>
  </head>
  <body>
    <div>
      <a id="close_button">close</a>
    </div>
    <h1>MyPanel</h1>
    <div id="div1">div1</div>
    <div id="div2">div2</div>
  </body>
</html>

prompt.js

// Wire-up an on click event listener.
document.getElementById("close_button").addEventListener("click", function() {
  // Emit a "close" message to main.
  self.port.emit("close");
});

关于javascript - 添加按钮以关闭面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24632069/

相关文章:

javascript - float 条形图的独特颜色

css - 可以从外部 .css 文件实现本地样式吗?

javascript - 基于 api key 的 Node JS Web 服务

javascript - 将 html 放在 javascript 字符串中?

image - 如何让响应式图片和字幕和谐相处?

JavaScript/jQuery : Monitor change to element?

javascript - 查找两个日期之间的差异代码在 Firefox(版本 40.0.3)中不起作用

javascript - 如何捕获源 self 自己的 Firefox Web 扩展的流量

javascript - 有没有一种方法可以检测 Firefox 是否会在 IE 选项卡中打开特定的 URL?

javascript - 框架、脚本和加载顺序