Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态添加的样式表仅在第一个样式表_Javascript_Css_Macos_Cocoa_Wkwebview - Fatal编程技术网

Javascript 动态添加的样式表仅在第一个样式表

Javascript 动态添加的样式表仅在第一个样式表,javascript,css,macos,cocoa,wkwebview,Javascript,Css,Macos,Cocoa,Wkwebview,我正在WKWebView中动态添加两个样式表,使用loadFileURL加载本地内容 cocoa应用程序调用以下javascript片段来添加样式表 var link = document.createElement('link'); link.setAttribute('rel', 'stylesheet'); link.type = 'text/css'; link.title = csstype; link.href = cssSheetPath1; document.head.appen

我正在
WKWebView
中动态添加两个样式表,使用
loadFileURL
加载本地内容

cocoa应用程序调用以下javascript片段来添加样式表

var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.type = 'text/css';
link.title = csstype;
link.href = cssSheetPath1;
document.head.appendChild(link);

var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.type = 'text/css';
link.title = csstype;
link.href = cssSheetPath2;
document.head.appendChild(link);
这两个样式表位于同一目录中。我可以加载第一个或第二个,但不能同时加载两个。如果我尝试同时加载这两个,则只应用第一个


你知道问题来自哪里吗?

你必须运行两次。创建一个函数,然后为两个样式表调用它

var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.type = 'text/css';
link.title = csstype;
link.href = cssSheetPath1;
document.head.appendChild(link);

var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.type = 'text/css';
link.title = csstype;
link.href = cssSheetPath2;
document.head.appendChild(link);
const createLink=(cssSheetPath)=>{
const link=document.createElement('link');
setAttribute('rel','stylesheet');
link.type='text/css';
link.href=cssSheetPath;
document.head.appendChild(链接);
}
createLink('style1.css');
createLink('style2.css');

添加了一个运行代码示例:


如果不使用函数,则应尝试使用两个不同的变量名称:

var link=document.createElement('link');
setAttribute('rel','stylesheet');
link.type='text/css';
link.title=csstype;
link.href=cssSheetPath1;
document.head.appendChild(链接);
var link1=document.createElement('link');
link1.setAttribute('rel','stylesheet');
link1.type='text/css';
link1.title=csstype;
link1.href=cssSheetPath2;
文件头附件子项(链接1);

我无法用WKWebView测试它

这正是我正在做的,而什么不起作用,因此我的问题是,在示例中只定义一个链接元素。因此,当您尝试将此元素用于两个样式表时,您将覆盖它。谢谢,我知道它在浏览器中工作。我的问题是WKWebView,第一个视图只有一个。(如果我也交换它们)问题仍然是samei发现的这个->。也许有帮助。