文字附加WebEngine - JavaFX的 [英] Append text WebEngine - JavaFX

查看:464
本文介绍了文字附加WebEngine - JavaFX的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何文本追加到webengine?
我试试这个

How can I append text to webengine? I try this

    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);
    }

但文件为空。

推荐答案

您还可以使用JavaScript做追加。

You can also use JavaScript to do the append.

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接口。

I sometimes find it simpler to use jQuery to manipulate the DOM rather than the Java Document or native JavaScript DOM interfaces.

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的出色答卷其他点的仍然适用。

Whether you use the Java Document API, as Uluk has, or JavaScript or JQuery APIs, all of the other points in Uluk's excellent answer still apply.

这篇关于文字附加WebEngine - JavaFX的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆