【问题标题】:Append text WebEngine - JavaFX附加文本 WebEngine - JavaFX
【发布时间】:2012-11-23 01:38:40
【问题描述】:

如何将文本附加到网络引擎?我试过这个:

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

【问题讨论】:

    标签: dom text append javafx document-body


    【解决方案1】:

    首先,如果 web 引擎无法加载内容,或者您​​在内容完全完成加载之前调用 engine.getDocument(),Document 将返回 null。

    其次,doc.getElementById("body") 搜索 id 为“body”的 DOM 元素。但是,您加载的内容根本没有这样的 id 或任何 id。

    为了更好地理解这些,这里有一个完整的可运行示例,点击按钮:

    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。

    【讨论】:

      【解决方案2】:

      您也可以使用 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 Document 或原生 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 出色答案中的所有其他要点仍然适用。

      【讨论】:

      • 嘿,如果我想使用该 webEngine (javafx) 的 jquery 删除网站外观的几张图像,你介意分享指导@jewelsea ....
      【解决方案3】:

      虽然答案很古老并且已经被接受,但我将我的发现放在这里。

      诀窍是在控制器的 init 方法中调用 engine.loadContent,然后尝试将内容附加到示例 btn.setOnAction() 中提到的某些操作上。这样,当您触发操作时,页面已经加载。如果我将加载代码放入 setOnAction() 本身,我会得到文档为 null。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-12
        • 2015-11-23
        • 1970-01-01
        • 1970-01-01
        • 2013-07-07
        • 1970-01-01
        相关资源
        最近更新 更多