【发布时间】:2018-03-02 14:03:57
【问题描述】:
注意:这是关于 JavaFX WebView,而不是 Android WebView(即我见过“Android Webview Anchor Link (Jump link) not working”)。
我在 javafx.scene.web.WebView 内显示一个生成的 HTML 页面,其中包含锚点和指向这些锚点的链接,如下所示:
<p>Jump to <a href="#introduction">Introduction</a></p>
some text ...
<h1 id="introduction">Introduction</h1>
more text ...
我使用这段代码将 HTML 加载到 WebView 中:
public void go(String location) {
try {
// read the content into a String ...
String html = NetUtil.readContent(new URL(location), StandardCharsets.UTF_8);
// ... and use loadContent()
webview.getEngine().loadContent(html);
} catch (IOException e) {
LOG.error(e);
}
}
一切都正确呈现,但如果我点击名为“介绍”的链接,什么都没有发生。
但是 HTML 是正确的,我使用以下代码进行了检查:
public void go(String location) {
// use load() to directly load the URL
webview.getEngine().load(location);
}
现在,一切正常。
问题似乎是因为使用loadContent()时WebView的文档URL是null,但由于它是只读属性,我不知道如何使它工作。
我需要使用loadContent(),因为 HTML 是动态生成的,如果可能的话,我不想为了使锚链接工作而将其写入文件。有没有办法解决这个问题?
编辑 我为 JavaFX 提交了bug。
【问题讨论】: