javascript - Firefox 会默默地忽略一些不违法的 CSS 值

标签 javascript firefox css

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 9 年前

我想通过 document.styleSheets 获取 styleSheets,但是,Firefox 似乎会默默地忽略一些并非非法的 CSS 值。我现在正在使用 vh & vw ,我想获取 css 值并为其生成正确的 px。

有没有办法让 Firefox 忽略这些值?

最佳答案

我确实认为 Firefox(可能还有其他浏览器)只会返回样式表中计算出的样式。

但是,您可以进入一个更加手动的过程,选择您想要的标签,发出 ajax 请求以获取其文本内容(几乎是即时的,因为它在缓存中),然后解析它。

html:

<link rel="stylesheet" href="style.css" id="css">

js(使用 jQuery):

$.get($("#css").attr("href"), function(data) //requests the css in ajax
{
   var selector = "div#right", rules = {};

   data = data.substring(data.indexOf(selector)); //trim all that is before your selector
   data = data.substring(data.indexOf("{") + 1, data.indexOf("}")); //get only what's in the curly braces
   data = data.split(";"); //split to get an array with each cell is a css rule

   for (var i = 0, l = data.length; i < l; i++) //loop through
   {
        data[i] = data[i].replace(/^\s+/, ''); //trim spaces before
        data[i] = data[i].replace(/\s+$/, ''); //trim spaces after

        if (data[i].indexOf(':') == -1) { continue; } //if rule is invalid, skip it

        data[i] = data[i].split(':'); //split rule so you have a sub-array with 0:key and 1:value
        rules[data[i][0]] = data[i][1]; //apply it to an object
   }

   console.dir(rules); //here you have. if your rules have "-" in it, you can't access with directly, but with brackets it'll be ok. ie : rules.margin-left is wrong but you can with rules["margin-left"].

}, "text");

关于javascript - Firefox 会默默地忽略一些不违法的 CSS 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610351/

上一篇:css - 使用 LESS CSS 在父元素中选择元素

下一篇:javascript - 使用多个下拉菜单更改 p 标签类

相关文章:

Javascript:从图像 URL 获取 Base64 字符串

javascript - onMouseover 在 Firefox 中不起作用

javascript - 如何识别 Firefox 中的 http 请求是谁发起的?

javascript - 如何调试javascript?

javascript - 是否可以将 html、css、js 等添加到 Go 服务器?

html - 显示: block css 在 div 元素中不起作用?

html - 有针对性的横向溢出

javascript - 在方法 GET 上从没有 "/"的日期选择器发送日期参数

javascript - Angularjs:大于带有 ng-repeat 的过滤器

javascript - 进行自动发送表单并打开另一个选择的选择(CodeIgniter)