我使用超链接在默认浏览器中打开 HTML 文件:
Hyperlink link = new Hyperlink("apple");
Path path = Paths.get("C:/Users/ExampleUser/Documents/fruits.html");
link.setOnAction(evt -> {
getHostServices().showDocument(path.toString());
});
效果很好。但是,我想使用可用 ID 在特定点打开文档,并立即跳转到“C:/Users/ExampleUser/Documents/fruits.html#apple”。这可能吗?
最佳答案
您可以直接调用 showDocument()
方法并添加 anchor :
getHostServices().showDocument("file:///C:/Users/ExampleUser/Documents/fruits.html#apple");
但是你必须添加URI方案,否则该方法将打开路径
file://C:/Users/ExampleUser/Documents/fruits.html%23apple
这显然不是您要打开的 URI。
关于java - 如何使用哈希标记显示文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55211884/