- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
?-6ren"> ?-在调查使用 附加 CSS 的优缺点时处理指令,我遇到了一些问题。 假设我们有一个简单的 XHTML 文档(以 application/xhtml+xml MIME 类型提供并在 Web 浏览器中查看-6ren">
在调查使用 <?xml-stylesheet>
附加 CSS 的优缺点时处理指令,我遇到了一些问题。
假设我们有一个简单的 XHTML 文档(以 application/xhtml+xml
MIME 类型提供并在 Web 浏览器中查看):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>A sample XHTML document</title>
<script type="application/javascript" src="/script.js"></script>
</head>
<body>
<h1>A heading</h1>
</body>
</html>
然后我们有一个外部 CSS 文件(让它命名为 style.css
并放在根目录中):
h1 { color: red; }
起初,在script.js
,我用 link
动态附加此 CSS元素:
const link = document.createElement('link');
Object.entries({rel: 'stylesheet', type: 'text/css', href: '/style.css'})
.forEach(([name, value]) => link.setAttribute(name, value));
document.head.appendChild(link);
然后脚本等待样式表完成加载并通过sheet
到达它。属性:
link.addEventListener('load', function() {
const stylesheet = link.sheet;
});
在此之后,脚本可以操作这个样式表,例如:
stylesheet.cssRules.item(0).style.color = 'green'; // modify an existing rule
stylesheet.insertRule('body { background: #ffc; }', 1); // insert a new rule
但是现在,如果样式表附加了 <?xml-stylesheet>
,我不知道是否可以进行相同的操作。处理指令:
const pi = document.createProcessingInstruction('xml-stylesheet',
'href="/style.css" type="text/css"');
document.insertBefore(pi, document.documentElement);
首先,PI好像没有load
事件,因此脚本无法知道样式表何时准备就绪。其次,没有什么像sheet
属性(property),所以你不能调用pi.sheet
到达样式表。
有什么方法可以克服这些困难并从脚本到与 <?xml-stylesheet>
关联的样式表吗? PI?
最佳答案
First, PI seem not to have load event, so the script cannot know when the stylesheet is ready.
您可以使用 PerformanceObserver
检查请求和加载的资源。迭代 document
的节点, 检查 .nodeType
7
或 .nodeType
8
, 作为 ProcessingInstruction
节点可以有 comment
.nodeType
.得到 "resource"
性能条目的属性。解析 .nodeValue
在 href="URL"
处为 URL 过滤的节点, 检查值是否等于 "resource"
性能条目,然后检查是否 .styleSheet
.href
value 等于解析的 URL,如果解析的 URL 等于性能条目 "resource"
适当的值(value)。如果true
, 迭代 .cssRules
或 .rules
的 styleSheet
加载于 ProcessingInstruction
节点。
window.onload = () => {
let resource;
const observer = new PerformanceObserver((list, obj) => {
for (let entry of list.getEntries()) {
for (let [key, prop] of Object.entries(entry.toJSON())) {
if (key === "name") {
resource = prop;
var nodes = document.childNodes;
_nodes: for (let node of nodes) {
if (node.nodeType === 7 || node.nodeType === 8
&& node.nodeValue === pi.nodeValue) {
let url = node.baseURI
+ node.nodeValue.match(/[^href="][a-z0-9/.]+/i)[0];
if (url === resource) {
observer.disconnect();
// use `setTimeout` here for
// low RAM, busy CPU, many processes running
let stylesheets = node.rootNode.styleSheets;
for (let xmlstyle of stylesheets) {
if (xmlstyle.href === url && url === resource) {
let rules = (xmlstyle["cssRules"] || xmlstyle["rules"]);
for (let rule of rules) {
// do stuff
console.log(rule, rule.cssText, rule.style, xmlstyle);
break _nodes;
}
}
}
}
}
}
}
}
}
});
observer.observe({
entryTypes: ["resource"]
});
const pi = document.createProcessingInstruction('xml-stylesheet',
'href="style.css" type="text/css"');
document.insertBefore(pi, document.documentElement);
}
plnkr http://plnkr.co/edit/uXfSzu0dMDCOfZbsdA7n?p=preview
您还可以使用 MutationObserver
, setTimeout()
处理
low RAM, busy CPU, many processes running
window.onload = function() {
let observer = new MutationObserver(function(mutations) {
console.log(mutations)
for (let mutation of mutations) {
for (let node of mutation.addedNodes) {
if (node.nodeName === "xml-stylesheet") {
let url = node.baseURI
+ node.nodeValue.match(/[^href="][a-z0-9/.]+/i)[0];
setTimeout(function() {
for (let style of document.styleSheets) {
if (style.href === url) {
observer.disconnect();
// do stuff
console.log(style)
}
}
// adjust `duration` to compensate for device
// low RAM, busy CPU, many processes running
}, 500)
}
}
}
});
observer.observe(document, {
childList: true
});
const pi = document.createProcessingInstruction('xml-stylesheet',
'href="style.css" type="text/css"');
document.insertBefore(pi, document.documentElement);
}
plnkr http://plnkr.co/edit/AI4QZiBUx6f1Kmc5qNG9?p=preview
或者,使用 XMLHttpRequest()
或 fetch()
请求.css
文件,创建和追加 <style>
元素到 document
, 处理响应文本,设置 .textContent
的 style
要调整的元素 css
文本。
关于javascript - 处理 <?xml-stylesheet> 类似于 <link rel ="stylesheet">?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41497274/
我在尝试将网站图标和样式表添加到我正在尝试构建的个人网站时遇到问题。这是我目前拥有的: Super Chilun's Portfolio Hello World! favicon 部
我一直在和我的 friend 们一起从头开始建立一个论坛,只是为了好玩,我们开始看到机器人和爬虫过去了。我们遇到的问题是您可以加载包含四个回复的页面/post/1,并且每个回复都包含一个指向自身/re
在我的大部分网站中,我都有很多指向我的其他网站和其他外部网站的外部链接。我需要知道什么时候在网站中使用 rel="nofollow" 或 rel="external" 更好? 最佳答案 您可以使用 e
下图描述了ELF可重定位目标文件的格式: 我们知道 .rel.text 和 .rel.data 部分包含链接器需要重定位以生成最终可执行文件的重定位条目。 我的问题是,为什么要区分.rel.text
我有一个带有上一个和下一个链接的页面: 如果用户按向左箭头或向右箭头,是否可以使用 Javascript 来导航这些链接?如果他们正在编辑文本并且按下向左或向右箭头,则不应激活该链接。 这将用于简
我经常看到以下模式: " target="_blank" rel="noopener noreferrer"> 但据我所知,noreferrer暗示 noopener 的影响.那么为什么是 noope
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve
我正在尝试对网站上的分页控件执行一些 SEO。 在 Google guidelines 之后,我想添加链接 rel=next/prev 即 到 我的搜索结果页面。 不幸的是,由于 MVC3 的设计决策
我正在尝试为我的 HTML5 站点实现一个为 XHTML 1.0 文档类型编写的 JQuery 图片库,问题是我不能在 HTML5 中使用自定义“rel”属性: tag --> $(documen
正如我在搜索结果中看到的许多丰富网页摘要标记,他们使用个人(个人)Google+ 个人资料完成了这些标记。那太好了。 那么,如果我使用我的 Google+ 企业品牌个人资料,是否合法。 请查看 Goo
我有一个页面,其中内置了基于 ajax 的分页。分页用于页面中的“评论”功能。根据 Google 的网站管理员博客,具有 rel="next"和 rel="prev"值有利于 SEO。 我在头部添加了
我们的网站目前正在使用“Yoast 的 WordPress SEO” rel="next" 和 rel="prev" 在类别和存档页面上工作正常,但是在我们创建的页面模板中,rel="next" 和
我想知道是因为我想存储除通常分配给 rel 的预定义关键字之外的其他内容……我只是想知道这是否是有效的 XHTML Strict。 最佳答案 据我所知,在 DTD(可以下载 from here )中:
我们有很多与 rel 的链接属性: One Two Three Four Five 还有一个ul : Some text Some text Some text Some te
只是为了确保它可以安全地与 JQuery 一起使用脚本。 最佳答案 它是 HTML 4.01 规范的一部分,只能用作 a 和 link 标签的属性(参见 the spec )。在 a 和 link 标
我在 PHPBB 论坛上使用移动模板 HTML 文件。我在 http://validator.w3.org/ 测试了 html 是否有错误测试结果出现如下错误 第 24 行,第 66 列:{navli
我注意到浏览器根本不使用“rel”属性,这是否使其成为存储 javascript 附加信息的理想位置(例如,删除 ajax 请求可以从 rel 中读取 id) 最佳答案 我假设您正在寻找一种方法来存储
我正在尝试验证我在其中实现了 Lightbox 的页面,但 W3C 认为 rel="lightbox['gallery']"是无效代码。代码是: ...image... 错误: Bad val
我可以为 rel 属性设置多个值吗?像这样: Link .. 它是否有效且跨浏览器兼容? 最佳答案 It's valid.不过,我不确定是否所有浏览器都支持它,但我猜是的。 rel = link-ty
我第一次尝试 rel=preload,将它用于几个样式表。这是有问题的代码: 我正在 Chrome 61 中进行测试,我可以看到样式表已按预期下载,但它们从未真正应用过,并且我在控制台上收到消息说
我是一名优秀的程序员,十分优秀!