- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想通过单击两个按钮来打开和关闭一个窗口。打开按钮打开窗口,关闭按钮关闭窗口。简单!
我通过两个独立函数的帮助实现了这一点。你们猜对了。一个用于打开,另一个用于关闭。
但是,这涉及将 myWindow
对象设为全局。为了避免这种情况,我在想闭包是否可以派上用场,将 myWindow
对象的范围限定为手动定义的函数。
我尝试执行类似下面粘贴的代码的操作,但不确定如何使其工作。现在 publicClose
未定义。
再次强调,我的重点是避免任何全局声明。
<html>
<head>
<meta charset="UTF-8">
<title>Day 5-6</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div class="buttons">
<ul class="list-inline">
<li><button class="btn btn-default" onclick="openWindow()">Open Window</button></li>
<li><button class="btn btn-default" onclick="publicClose()">Close Window</button></li>
</ul>
</div>
<script type="text/javascript">
function openWindow(){
var myWindow = windowCenter("", "", 400, 200 );
function windowCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var ScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var ScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + ScreenLeft;
var top = ((height / 2) - (h / 2)) + ScreenTop;
var myWindow = window.open("", "", 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
myWindow.document.write("<strong>I am child window.</strong>")
// Puts focus on the newWindow
if (window.focus) {
myWindow.focus();
}
}
function closeWindow(){
myWindow.close();
}
var publicClose = {
close: closeWindow;
}
return publicClose;
}
</script>
</body>
</html>
最佳答案
工作副本来了
<title>Day 5-6</title>
<script type="text/javascript">
var closeWindow;
function openWindow() {
var myWindow;
windowCenter("", "", 400, 200);
function windowCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var ScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var ScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + ScreenLeft;
var top = ((height / 2) - (h / 2)) + ScreenTop;
myWindow = window.open("", "", 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
myWindow.document.write("<strong>I am child window.</strong>");
// Puts focus on the newWindow
if (window.focus) {
myWindow.focus();
}
}
closeWindow = function() {
myWindow.close();
}
}
function publicClose() {
closeWindow();
}
</script>
<body>
<div class="buttons">
<ul class="list-inline">
<li>
<button class="btn btn-default" onclick="return openWindow();">Open Window</button>
</li>
<li>
<button class="btn btn-default" onclick="publicClose()">Close Window</button>
</li>
</ul>
</div>
</body>
有哪些变化?我没有返回一个对象,而是将 close window 分配给一个全局变量,这创建了 openWindow
函数的闭包。
在windowCenter
函数中,myWindow
不是一个新变量,而是来自openWindow
上下文的变量。我没有从 windowCenter
函数返回任何内容
从 openWindow
上下文中删除了 publicClose
,并在全局上下文中添加了 publicClose
函数。但这是可以避免的,并且可以通过单击按钮直接调用 closeWindow
关于javascript - 打开和关闭带有关闭装置的 window ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35126009/
我一直致力于将两个遗留数据库中的一些 57k 多条记录精炼和重构为一个与 Django 兼容的实体。现在,当我完成后,我将其转储为固定装置,并尝试将其加载到生产环境中。 我的问题是该进程不久后就被“杀
我有一个包含 3 个模型的应用程序,它们以父子方式相互引用: class A(Model): # ... class B(Model): a = ForeignKey(A) #
我在我的 Django 应用程序中使用夹具,但只有两个应用程序正在加载它们的夹具。 当我使用 --verbosity=2 手动运行 loaddata 时,我可以看到它只在两个应用程序中查找,尽管我在其
我正在寻找一个快速的 BigFloat 单元,它可以处理加法、减法、乘法和除法(对数可以,但不是必需的),并且精度至少为 100 位小数。我试过this单位,但它比标准扩展操作慢大约 1,000 倍。
我有一个 Django 应用程序。我有包含测试数据的 .json 固定文件,以及使用数据确认应用程序正常工作的单元测试。我还使用 South 来迁移我的数据库。 在进行了几次数据库迁移后,我的固定装置
我创建了一些模型,我想为其提供初始数据。问题是模型有好几个,我想整理一下数据。 目前,我有一个包含数据的大 JSON 文件:initial_data.json。我以为我可以使用一些注释,但是 JSON
我的项目中有以下文件树: ... tests/ __init__.py test_abc.py ... 我在 __init__.py 中定义了一个固定装置: @pytest.fixtu
目前,我所有的装置都与它们要用于的 table 同名,因为最近出现了 rails 问题,似乎不可能有一个以“test”这个词开头的装置 有谁知道有一种方法可以使用不同的灯具名称,然后将其映射到正确的表
我正在尝试向使用多个数据库的 rails (v 3.1.3) 应用程序添加测试装置。 该夹具应仅应用于应用程序自己的测试 sqlite 数据库: test: adapter: sqlite3
我正在尝试在Docker容器中运行riofs,但是当我尝试运行riofs时,出现以下错误: fuse: device not found, try 'modprobe fuse' first ERRO
我制作了一个基本包,以方便在 Pyramid 应用程序中使用 SQLAlchemy:pyramid_sqlalchemy 。当您使用此包为应用程序编写测试时,您将需要一些固定装置来提供工作数据库环境。
有没有办法将我现有的数据库行导出为 Doctrine 装置? 最佳答案 不幸的是,目前没有 symfony 1.x 中的导出功能 关于doctrine - Symfony2 将数据库导出到 Doctr
我正在尝试创建一组测试用例来覆盖我的 Django 应用程序。我需要预定义的数据库数据来运行我的一些测试。所以我决定使用固定装置。 我所做的是:1)创建 fixture 文件: oleg$ pytho
我有以下结构: demo/ conftest.py test_1.py test_2.py conftest.py 内容为: import pytest @pytest.fixture()
有人在 Windows 上安装过 Polymer 吗?我刚开始使用 GIT,它说你需要 Bower 和 Node 以及其他一些东西才能让它工作。 最佳答案 我们正在开发一个简单的 ZIP 文件供人们下
Python 和 Django 的新手,尝试导入 .json 文件。 JSON 文件的结构使得每个根级对象中都有一个嵌套对象。 例如,JSON 的结构为: [ { "model": "test
我正在尝试理解提供的示例 here并模拟它适用于 post 方法而不是 get 方法。 当我测试 response.status_code == 200 时,一切正常。如何在不重复代码的情况下测试多个
我正在使用 jasmine fixtures,我想用一个包含 iframe 的 HTML 编写一个测试。 问题是测试在我的 iframe 加载之前执行。 库本身有解决方案吗? 还是我必须实现自己的机制
我有一个基于 Symfony2 + Doctrine2 构建的应用程序,我想为其创建一些测试(使用 phpunit)。 例如,如果我想针对数据库中的记录测试一个唯一验证器,我想创建一个我可以使用的记录
如何在 .yml 中为 has_many 和 belongs_to 变量创建示例数据。 这是将这些文件添加到终端中一个简单的 rails new lab 命令的示例。我真的不知道如何用英语解释这个。但
我是一名优秀的程序员,十分优秀!