如何让WebKit WebView使用CSS样式表? [英] How do I make a WebKit WebView use a CSS style sheet?
问题描述
我制作了一个HTML链接到一个CSS文件,在浏览器中打开它,并且样式正确显示。
然后我加载到WebView,样式不显示。
我甚至尝试在Objective-C的DOM中插入一个< link>
,这是我的最终目标,但都不工作。
我必须在webView上启用CSS吗?
I made an html wich links to a CSS file, open it in browsers, and the styles show correctly.
Then I load it in a WebView and the styles don't show.
I even tried to insert a <link>
into the DOM from Objective-C, which is my ultimate goal, but neither worked.
Do I have to enable CSS on the webView somehow?
编辑:
CSS包含在html中:
< link rel ='StyleSheet'href =file://localhost/Users/petruza/Source/Scrapp/build/Debug/Scrapp.app/Contents/Resources/Scrapp.csstype = text / css>
在DOM中插入的CSS :(我选中并插入)
the CSS inserted in the DOM: (I checked and it does get inserted)
NSURL *cssUrl = [[NSBundle mainBundle] URLForResource:@"Scrapp.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:
在我的WebView和Safari中显示的完全相同的HTML: br>
edit2:
Same exact html shown in my WebView and in Safari:
推荐答案
您可能需要包含一个html < base>
元素来告诉webview它在哪里寻找css文件。
You might need to include an html <base>
element to tell the webview where it shoul be looking for the css files.
这篇关于如何让WebKit WebView使用CSS样式表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!