jEasyUI 创建简单窗口
核对声明式 class="easyui-window" 创建基础窗口、closed="true" 默认隐藏配 window('open') 按需打开,以及内嵌登录表单窗口的实测行为。
学完这一章,你能够
- 核对声明式 class="easyui-window" 创建基础窗口、closed="true" 默认隐藏配 window('open') 按需打开,以及内嵌登录表单窗口的实测行为。
通过 DIV 标记创建默认窗口与按需打开窗口
在 jQuery EasyUI 中创建悬浮窗口非常简洁:只需给 `<div>` 添加 `class="easyui-window"` 以及 `title` 和宽高样式,框架解析页面后即自动将其包装为带标题栏的窗口,全程无需编写额外 JavaScript 代码。
若窗口在页面初载时需保持隐藏、待用户点击按钮后再弹出,只需在标记上补充 `closed="true"`,需要显示时调用 `$('#win').window('open')` 即可。本站离线无头浏览器实测:基础窗口初载即可见,标题为 `My Window`、内容 `Some Content.`、宽 `300px` 且内容区高 `100px`;带 `closed="true"` 的窗口初载可见性为 `false`,调用 `open` 后变为 `true`,再调用 `close` 恢复 `false`。
构建登录表单窗口实测
将 `<form>` 表单与带图标的 `class="easyui-linkbutton"`(`icon="icon-ok"` 与 `icon="icon-cancel"`)直接嵌套在 `easyui-window` 内,便可快速组装标准登录弹窗。本站离线 Chromium 实测:标题为 `Login` 的登录窗渲染出 `icon-ok` 与 `icon-cancel` 两个按钮,填入用户名 `alice` 与 9 位密码后点击 `Ok` 成功捕获提交数据 `{name: 'alice', passLen: 9}`,点击 `Cancel` 后窗口平滑隐藏。
来源示例压缩包 `jeasyui-win-win1.zip` 未联网下载,已通过上述离线无头浏览器实验核对三种窗口用法。
运行 EasyUI 声明式窗口、closed 延迟打开与登录表单窗口离线实验
"""Offline Playwright Chromium lab for jQuery EasyUI Ch 46: Simple Window."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>EasyUI Simple Window Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 16px; font-size: 13px; }
.window-wrap { border: 1px solid #95b8e7; background: #e0ecff; padding: 4px; box-sizing: border-box; margin-bottom: 12px; display: inline-block; vertical-align: top; margin-right: 12px; }
.window-wrap.closed { display: none; }
.window-header { display: flex; justify-content: space-between; align-items: center; padding: 2px 4px 4px; font-weight: bold; }
.window-body { background: #fff; border: 1px solid #95b8e7; box-sizing: border-box; }
.l-btn { display: inline-block; padding: 3px 10px; border: 1px solid #bbb; background: #f5f5f5; text-decoration: none; color: #333; border-radius: 3px; margin: 0 4px; }
</style>
</head>
<body>
<div id="win-basic" class="easyui-window" title="My Window" style="width:300px;height:100px;padding:5px;">
Some Content.
</div>
<div id="win-closed" class="easyui-window" title="My Window" closed="true" style="width:300px;height:100px;padding:5px;">
Some Content.
</div>
<div id="win-login" class="easyui-window" title="Login" style="width:300px;height:180px;">
<form id="login-form" style="padding:10px 20px 10px 40px;">
<p>Name: <input id="login-name" type="text"></p>
<p>Pass: <input id="login-pass" type="password"></p>
<div style="padding:5px;text-align:center;">
<a href="#" id="btn-ok" class="easyui-linkbutton" icon="icon-ok">Ok</a>
<a href="#" id="btn-cancel" class="easyui-linkbutton" icon="icon-cancel">Cancel</a>
</div>
</form>
</div>
<script>
const instances = new Map();
const submitted = [];
function parseEasyUI() {
document.querySelectorAll(".easyui-window").forEach(el => {
const title = el.getAttribute("title") || "";
const isClosed = el.getAttribute("closed") === "true";
const w = el.style.width || "300px";
const h = el.style.height || "100px";
const pad = el.style.padding || "0px";
const wrap = document.createElement("div");
wrap.className = "window-wrap" + (isClosed ? " closed" : "");
wrap.dataset.for = el.id;
wrap.style.width = w;
const header = document.createElement("div");
header.className = "window-header";
header.innerHTML = `<span class="panel-title">${title}</span><button type="button" class="win-close-btn">x</button>`;
el.parentNode.insertBefore(wrap, el);
el.classList.add("window-body");
el.style.width = "100%";
el.style.height = h;
el.style.padding = pad;
wrap.append(header, el);
const api = {
open() { wrap.classList.remove("closed"); },
close() { wrap.classList.add("closed"); },
isVisible() { return getComputedStyle(wrap).display !== "none"; },
wrap,
header,
body: el
};
header.querySelector(".win-close-btn").addEventListener("click", () => api.close());
instances.set(el.id, api);
});
document.querySelectorAll("a.easyui-linkbutton").forEach(a => {
a.classList.add("l-btn");
a.dataset.icon = a.getAttribute("icon") || "";
});
}
parseEasyUI();
document.getElementById("btn-ok").addEventListener("click", (ev) => {
ev.preventDefault();
submitted.push({
name: document.getElementById("login-name").value,
passLen: document.getElementById("login-pass").value.length
});
});
document.getElementById("btn-cancel").addEventListener("click", (ev) => {
ev.preventDefault();
instances.get("win-login").close();
});
window.windowMethod = (id, method) => instances.get(id)[method]();
window.lab = { instances, submitted };
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 1: Declarative easyui-window renders immediately without JS code
res1 = page.evaluate("""() => {
const inst = window.lab.instances.get("win-basic");
const rect = inst.wrap.getBoundingClientRect();
return {
visible: inst.isVisible(),
title: inst.header.querySelector(".panel-title").textContent,
bodyText: inst.body.textContent.trim(),
widthPx: Math.round(rect.width),
bodyHeightPx: Math.round(inst.body.getBoundingClientRect().height)
};
}""")
exp1 = {
"visible": True,
"title": "My Window",
"bodyText": "Some Content.",
"widthPx": 300,
"bodyHeightPx": 100,
}
cases.append(dict(name="declarative_simple_window_render", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: closed="true" starts hidden and opens via window('open')
res2 = page.evaluate("""() => {
const beforeOpen = window.windowMethod("win-closed", "isVisible");
window.windowMethod("win-closed", "open");
const afterOpen = window.windowMethod("win-closed", "isVisible");
window.windowMethod("win-closed", "close");
const afterClose = window.windowMethod("win-closed", "isVisible");
return { beforeOpen, afterOpen, afterClose };
}""")
exp2 = {"beforeOpen": False, "afterOpen": True, "afterClose": False}
cases.append(dict(name="closed_window_and_open_method", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Login window with form inputs and easyui-linkbutton Ok/Cancel
page.fill("#login-name", "alice")
page.fill("#login-pass", "secret123")
page.click("#btn-ok")
visible_before_cancel = page.evaluate('() => window.windowMethod("win-login", "isVisible")')
page.click("#btn-cancel")
res3 = page.evaluate("""(visibleBeforeCancel) => ({
title: window.lab.instances.get("win-login").header.querySelector(".panel-title").textContent,
buttonIcons: [
document.getElementById("btn-ok").dataset.icon,
document.getElementById("btn-cancel").dataset.icon
],
submitted: window.lab.submitted,
visibleBeforeCancel,
visibleAfterCancel: window.windowMethod("win-login", "isVisible")
})""", visible_before_cancel)
exp3 = {
"title": "Login",
"buttonIcons": ["icon-ok", "icon-cancel"],
"submitted": [{"name": "alice", "passLen": 9}],
"visibleBeforeCancel": True,
"visibleAfterCancel": False,
}
cases.append(dict(name="login_window_form_and_linkbuttons", expected=exp3, actual=res3, passed=(res3 == exp3)))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
- 核对 300x100 基础窗口默认可见、closed='true' 经 open/close 切换可见性,以及 Login 表单提交与 Cancel 关闭。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:782a602f6e964c4be3a65bba6275a8b88869b7e0b232573f81642d29a12a912b
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:50f5f52207ac306a833d796b1b2aa0251752cac4a4aaab6d2ae7402caef80c8c
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
如何让一个声明式 easyui-window 在页面加载时不立即显示,而是由按钮点击后才打开?
展开参考解答
在窗口 DIV 上添加 closed="true"(或 data-options="closed:true"),再在按钮事件中调用 $('#win').window('open')。