我目前正尝试在webview中呈现单张音乐HTML (我用javascript库生成的HTML)。这是我的代码:
ViewController.swift (部分):
let web = WKWebView()
let localfilePath = NSBundle.mainBundle().URLForResource("index", withExtension: "html")
let myRequest = NSURLRequest(URL: localfilePath!)
web.loadRequest(myRequest)index.html:
<html>
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="http://www.vexflow.com/support/vexflow-min.js"></script>
<script src="http://www.vexflow.com/support/tabdiv-debug.js"></script>
<body>
<h1 id="someElement"> Test </h1>
<div style="width:680; margin-left: auto; margin-right: auto;">
<div class="vex-tabdiv">
options width= 700 space=20
tabstave notation=true key=A time=4/4
notes :q =|: (5/2.5/3.7/4) :8 7-5h6/3 ^3^ 5h6-7/5 ^3^ :q 7V/4 |
notes :8 t12p7/4 s5s3/4 :8 3s:16:5-7/5 :h p5/4
text :w, |#segno, ,|, :hd, , #tr
</div>
</div>
</body>
</html>虽然HTML确实加载,但它没有正确地呈现。如果我在Chrome中呈现这个HTML,它将如下所示:

然而,当我尝试在我的webview中运行它时,我得到:

如您所见,HTML是在没有JS的情况下呈现的。我已经搜索过Google/SO,并尝试过各种方法,比如将JS文件本地化,并将它们作为
<script src="lodash.js"></script>以及使用WKWebView的executeJavascript函数都没有用,但我肯定遗漏了一些东西。事先非常感谢!
发布于 2016-08-18 17:45:30
我发现了这个问题,这与两件主要的事情有关:
希望这对未来的其他人有所帮助!:)
发布于 2016-08-18 10:08:16
原因可能是您的div呈现时js文件正在加载。您可以将外部js文件保存到本地并引用这些本地文件。对我起作用了。
https://stackoverflow.com/questions/39011075
复制相似问题