Javascript 将WebView中DOM节点的内容绑定到StringProperty
我一直在尝试将WebView中html文档中的DOM节点的内容绑定到StringProperty。我拥有的是这样的东西:Javascript 将WebView中DOM节点的内容绑定到StringProperty,javascript,java,javafx,webview,Javascript,Java,Javafx,Webview,我一直在尝试将WebView中html文档中的DOM节点的内容绑定到StringProperty。我拥有的是这样的东西: final String html = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><script>function get(){return document.getElementsById('code')[0];}</script><s
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);
});
final String html=“function get(){return document.getElementsById('code')[0];}body{font-family:\“Monospaced\”;}”;
加载内容(html);
final JSObject pre=(JSObject)engine.executeScript(“get()”)//用html定义
addListener((ob,o,n)->{
pre.setMember(“innerHTML”,n);
});
当我执行程序并修改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);
});
线程“JavaFX应用程序线程”netscape.javascript.JSException:ReferenceError:找不到变量:get
我不确定我是否在正确的轨道上。似乎应该可以侦听属性中的更改,并使用javascript将这些更改推送到文档中。我做错什么了吗?Javascript中没有
getElementsById
函数。ID是唯一的。使用getElementById
(不是元素,也没有数组表示法),或者如果需要多个选择器,则使用另一个选择器
基于代码的JSFIDLE示例:异步加载
WebView
中的内容。您必须等待WebView
完成内容加载。为True,但不是此错误的原因。(错误消息提到的是get
,而不是getElementsById
)。我猜错误只是在脚本执行时没有加载页面。这是真的。也许转义引号时出错了?我知道我将该字符串复制到了JSFIDLE中,并得到了相同的错误。然后我清理了一切,包括getElementsById,它运行时没有出错。这可能是我改变的其他东西。