我制作了一个带有 CSS 文件链接的 html,在浏览器中打开它,样式显示正确。
然后我将其加载到 WebView 中,但样式不显示。
我什至尝试插入一个<link>
从 Objective-C 到 DOM,这是我的最终目标,但都没有成功。
我是否必须以某种方式在 webView 上启用 CSS?
edit:
CSS 包含在 html 中:
<link rel='StyleSheet' href="file://localhost/Users/petruza/Source/Scrape/build/Debug/Scrape.app/Contents/Resources/Scrape.css" type="text/css" >
插入到 DOM 中的 CSS:(我检查过,它确实被插入了)
NSURL *cssUrl = [[NSBundle mainBundle] URLForResource:@"Scrape.css" withExtension: nil];
DOMDocument* dom = [[web mainFrame] DOMDocument];
[window setTitleWithRepresentedFilename: lastRunScript];
DOMElement* link = [dom createElement:@"link"];
[link setAttribute:@"rel" value:@"StyleSheet"];
[link setAttribute:@"type" value:@"text/css"];
[link setAttribute:@"href" value: [cssUrl absoluteString]];
DOMElement* head = (DOMElement*) [[dom getElementsByTagName:@"head"] item:0];
DOMElement* headFirstChild = head.firstElementChild;
if( headFirstChild )
[head insertBefore:link refChild:(DOMNode *)headFirstChild];
else
[head appendChild:(DOMNode *)link];
edit2:
Same exact html shown in my WebView and in Safari:
![enter image description here](https://i.stack.imgur.com/T9KV9.png)
您可能需要包含一个 html<base>
告诉 webview 应该在哪里寻找 css 文件的元素。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)