【发布时间】:2016-01-14 00:58:19
【问题描述】:
我一直在尝试将 WebView 中的 html 文档内的 DOM 节点的内容绑定到 StringProperty。我所拥有的是这样的:
final String html = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><script>function get(){return document.getElementsById('code')[0];}</script><style>body{font-family:\"Monospaced\";}</style></head><body><pre id=\"code\"></pre></body></html>";
engine.loadContent(html);
final JSObject pre = (JSObject) engine.executeScript("get()"); // Defined in html
property.addListener((ob, o, n) -> {
pre.setMember("innerHTML", n);
});
当我执行程序并修改 StringProperty 时,我得到如下异常:
线程“JavaFX 应用程序线程”netscape.javascript.JSException 中的异常:ReferenceError:找不到变量:get
我不确定我是否走在正确的轨道上。似乎应该可以监听属性中的更改并使用 javascript 将这些更改推送到文档中。我做错了吗?
【问题讨论】:
-
WebView中的内容是异步加载的。您必须等待WebView完成加载内容。
标签: javascript java javafx webview