调用Javascript window.location.hash的Javafx WebEngine不起作用

时间:2016-02-09 10:04:44

标签: javascript java javafx javafx-webengine

我尝试了不同的解决方案,使我的webview指向文档中的特定锚点,但失败了。 我的目标名称是'occ' 我使用了window.location.hash = 'occ'window.location.href ='#occ',但我的代码

没有用
    contentView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
        public void changed(@SuppressWarnings("rawtypes") ObservableValue ov, State oldState, State newState) {
            if (newState == State.SUCCEEDED) {
                System.out.println("loaded");
                doc = contentView.getEngine().getDocument();
                JSObject windowObject = (JSObject) contentView.getEngine().executeScript("window");
                windowObject.call("scrollTo", 0, 600);//this works
                contentView.getEngine().executeScript("location.hash = 'occ'"); // doesn't work
                windowObject.call("location.hash", "occ");  // doesn't work
            }
        }
    });

这里是我创建锚点

的代码部分
org.jsoup.nodes.Document d = Jsoup.parse(new File(doc.get("path")), "utf-8");
String searchedPage = d.toString();
for (String keyword : keywordsArray) {
    searchedPage = searchedPage.replaceAll(keyword,
    "<a id='occ' name='occ'><span style='font-weight:bold; background:yellow'>" + keyword + "</span></a>");
 }
contentEngine.loadContent(searchedPage);

0 个答案:

没有答案