如何用jQuery获取CSS注释?

时间:2023-01-24 23:07:19

I am wondering how I can read CSS comments out of a linked stylesheet.

我想知道如何从链接的样式表中读取CSS注释。

I have this sample CSS loaded via:

我有这个示例CSS加载通过:

<link rel="stylesheet" type="text/css" media="all" href="test.css" />

 

#test1{ border:1px solid #000; }
#test2{ border:1px solid #000; }
#test3{/* sample comment text I'm trying to read */}

I'm testing this in FF3. The following javascript reads the rules but doesn't read the comments in#test3.

我在FF3中测试这个。以下javascript读取规则但不读取#test3中的注释。

window.onload = function(){
    s=document.styleSheets;
    for(i=0;i < s[0].cssRules.length;i++){
        alert(s[0].cssRules[i].cssText);
    }
}

5 个解决方案

#1


You could retrieve the stylesheet's contents and use regex to parse the comments. This example uses jQuery to get the stylesheet text and a regular expression to find the comments:

您可以检索样式表的内容并使用正则表达式来解析注释。此示例使用jQuery获取样式表文本和正则表达式以查找注释:

jQuery.get("test.css", null, function(data) {
    var comments = data.match(/\/\*.*\*\//g);
    for each (var c in comments) 
        alert(c);
});

You could also find the stylesheet links using selectors.

您还可以使用选择器找到样式表链接。

#2


Comments will almost always be ignored by an interpreter and therefor will not be available.

口译员几乎总是会忽略评论,因此无法提供。

#3


You can access the CSS file using an AJAX query and then parse the results yourself looking for comments. The interpreter won't get in the way then.

您可以使用AJAX查询访问CSS文件,然后自己解析结果以查找注释。那时翻译不会妨碍。

As long as the CSS is on the same domain as the page, this will work nicely.

只要CSS与页面位于同一个域中,这将很好地工作。

#4


You can't, that's the entire point of comments.

你不能,这是评论的全部内容。

#5


You can't read the CSS file JavaScript, just inspect the results in the DOM. One possible way might be to use an embedded stylesheet, where you can query the textual content of the style tag via the DOM interface. You have to parse the content for yourself, of course.

您无法读取CSS文件JavaScript,只需检查DOM中的结果即可。一种可能的方法是使用嵌入式样式表,您可以通过DOM界面查询样式标记的文本内容。当然,你必须为自己解析内容。

#1


You could retrieve the stylesheet's contents and use regex to parse the comments. This example uses jQuery to get the stylesheet text and a regular expression to find the comments:

您可以检索样式表的内容并使用正则表达式来解析注释。此示例使用jQuery获取样式表文本和正则表达式以查找注释:

jQuery.get("test.css", null, function(data) {
    var comments = data.match(/\/\*.*\*\//g);
    for each (var c in comments) 
        alert(c);
});

You could also find the stylesheet links using selectors.

您还可以使用选择器找到样式表链接。

#2


Comments will almost always be ignored by an interpreter and therefor will not be available.

口译员几乎总是会忽略评论,因此无法提供。

#3


You can access the CSS file using an AJAX query and then parse the results yourself looking for comments. The interpreter won't get in the way then.

您可以使用AJAX查询访问CSS文件,然后自己解析结果以查找注释。那时翻译不会妨碍。

As long as the CSS is on the same domain as the page, this will work nicely.

只要CSS与页面位于同一个域中,这将很好地工作。

#4


You can't, that's the entire point of comments.

你不能,这是评论的全部内容。

#5


You can't read the CSS file JavaScript, just inspect the results in the DOM. One possible way might be to use an embedded stylesheet, where you can query the textual content of the style tag via the DOM interface. You have to parse the content for yourself, of course.

您无法读取CSS文件JavaScript,只需检查DOM中的结果即可。一种可能的方法是使用嵌入式样式表,您可以通过DOM界面查询样式标记的文本内容。当然,你必须为自己解析内容。