2

在被 javafx webengine 加载后试图添加到 html 页面的内容。

webEngine.load(getClass().getResource("teachsticks/classroomscreen.html").toExternalForm());
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
    @Override
    public void changed(ObservableValue<? extends Worker.State> observable, 
                        Worker.State oldValue, Worker.State newValue) {

        if (newValue == Worker.State.SUCCEEDED) {
            JSObject win = (JSObject) webEngine.executeScript("window");
            win.setMember("app", new JavaApp());

            Document doc = webEngine.getDocument();
            JavaApp app = new JavaApp();

            // [...]

我最初尝试使用此方法附加但失败了:

Element elementNode = doc.createElement("div");
elementNode.setAttribute("id", "stat");
elementNode.appendChild(app.readHeading(app.buildContent(),doc));
Element el = doc.getElementById("content-1");
el.appendChild(elementNode);

我也在下面尝试了第二个。javascript函数包含在html页面上,但在执行脚本时遇到错误:

webEngine.executeScript("test('" + string + "');");

Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: SyntaxError: Unexpected EOF
    at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128)
    at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
    at com.sun.webkit.WebPage.executeScript(WebPage.java:1427)
    at javafx.scene.web.WebEngine.executeScript(WebEngine.java:948)
    at teachsticks_1.WebClientHandler.executejQuery(WebClientHandler.java:78)
    at teachsticks_1.WebClientHandler.access$100(WebClientHandler.java:63)
    at teachsticks_1.WebClientHandler$1.changed(WebClientHandler.java:134)
    at teachsticks_1.WebClientHandler$1.changed(WebClientHandler.java:111)
    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176)
    at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)

方法3也遇到同样的错误

String string = nodeToString(elementNode);
string = string.replace("xmlns=\"http://www.w3.org/1999/xhtml\"", "");
string = StringEscapeUtils.escapeHtml3(string);
System.out.println(string);
executejQuery(webEngine, "$('#j').append('"+string+"!');");

executejQuery()这里

要注入的字符串是:

<DIV id="stat" xmlns="http://www.w3.org/1999/xhtml">
    <UL>
        <LI data-jstree="{&quot;icon&quot;:&quot;http://jstree.com/tree.png&quot;}">Statistics
            <UL>
                <LI data-jstree="{&quot;icon&quot;:&quot;glyphicon glyphicon-ok-sign&quot;}">Data Collection</LI>
                <LI data-jstree="{&quot;icon&quot;:&quot;glyphicon glyphicon-ok-sign&quot;}">Introduction to Statistics</LI>
            </UL>
        </LI>
    </UL>
</DIV>

有什么方法可以实现。

4

0 回答 0