如何将文本追加到text引擎?我试过这个:
public TabMessage(String title) {
super(title);
view = new WebView();
engine = view.getEngine();
engine.loadContent("<body></body>");
view.setPrefHeight(240);
}
private void append(String msg){
Document doc = engine.getDocument();
Element el = doc.getElementById("body");
String s = el.getTextContent();
el.setTextContent(s+msg);
}但是文档是null
发布于 2012-12-06 02:54:32
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比使用JavaScript或原生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 + "});"
);
}无论您是像Uluk那样使用Java Document API,还是使用JavaScript或JQuery API,Uluk优秀答案中的所有其他要点仍然适用。
发布于 2012-12-05 18:06:08
首先,如果webengine无法加载内容,或者在内容完全加载之前调用engine.getDocument(),文档将返回null。
package demo;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Demo extends Application {
private WebView view;
private WebEngine engine;
@Override
public void start(Stage primaryStage) {
view = new WebView();
engine = view.getEngine();
engine.loadContent("<body><div id='content'>Hello </div></body>");
view.setPrefHeight(240);
Button btn = new Button("Append the \"World!\"");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
append(" \"World!\"");
}
});
StackPane root = new StackPane();
root.getChildren().addAll(view, btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
private void append(String msg) {
Document doc = engine.getDocument();
Element el = doc.getElementById("content");
String s = el.getTextContent();
el.setTextContent(s + msg);
}
public static void main(String[] args) {
launch(args);
}
}请注意,我在主体中放置了一个带有id=content的div,因此doc.getElementById("content")将返回该div。
发布于 2018-02-06 15:12:27
虽然答案很古老,而且已经被接受,但我还是把我的发现放在了这里。
诀窍是在控制器的初始化方法中调用engine.loadContent,然后尝试将内容附加到某些操作上,如示例btn.setOnAction()中所述。通过这种方式,当您触发操作时,页面已经加载。如果我将用于加载的代码放在setOnAction()本身中,我得到的文档为空。
https://stackoverflow.com/questions/13719669
复制相似问题