gpt4 book ai didi

css - 如何让 WebKit WebView 使用 CSS 样式表?

转载 作者:技术小花猫 更新时间:2023-10-29 11:20:21 24 4
gpt4 key购买 nike

我制作了一个链接到 CSS 文件的 html,在浏览器中打开它,样式正确显示。
然后我将它加载到 WebView 中,但样式不显示。
我什至尝试插入 <link>从 Objective-C 进入 DOM,这是我的最终目标,但都没有奏效。
我是否必须以某种方式在 webView 上启用 CSS?

编辑:
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];

编辑2:
在我的 WebView 和 Safari 中显示的完全相同的 html:
enter image description here

最佳答案

您可能需要包含一个 html <base>元素告诉 webview 它应该在哪里寻找 css 文件。

关于css - 如何让 WebKit WebView 使用 CSS 样式表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com