您還可以使用JavaScript來完成附加操作。
final WebEngine appendEngine = view.getEngine();
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
appendEngine.executeScript(
"document.getElementById('content').appendChild(document.createTextNode('World!'));"
);
}
});
我有時會發現使用jQuery來操縱DOM而不是Java文檔或本機JavaScript DOM接口更簡單。
final WebEngine appendEngine = view.getEngine();
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
executejQuery(appendEngine, "$('#content').append('World!');");
}
});
...
private static Object executejQuery(final WebEngine engine, String script) {
return engine.executeScript(
"(function(window, document, version, callback) { "
+ "var j, d;"
+ "var loaded = false;"
+ "if (!(j = window.jQuery) || version > j.fn.jquery || callback(j, loaded)) {"
+ " var script = document.createElement(\"script\");"
+ " script.type = \"text/javascript\";"
+ " script.src = \"http://code.jquery.com/jquery-1.7.2.min.js\";"
+ " script.onload = script.onreadystatechange = function() {"
+ " if (!loaded && (!(d = this.readyState) || d == \"loaded\" || d == \"complete\")) {"
+ " callback((j = window.jQuery).noConflict(1), loaded = true);"
+ " j(script).remove();"
+ " }"
+ " };"
+ " document.documentElement.childNodes[0].appendChild(script) "
+ "} "
+ "})(window, document, \"1.7.2\", function($, jquery_loaded) {" + script + "});"
);
}
無論您使用Java API文檔,如Uluk擁有或JavaScript或JQuery的的API,所有Uluk的出色答卷其他點的仍然適用。
嘿,如果我想刪除使用該webEngine(javafx)的jQuery的網站外觀的幾個圖像,你會介意分享指導@ jewelsea .... – gumuruh