首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性'insertRule‘和'deleteRule’在'StyleSheet‘类型上不存在

属性'insertRule‘和'deleteRule’在'StyleSheet‘类型上不存在
EN

Stack Overflow用户
提问于 2020-01-20 07:45:00
回答 1查看 394关注 0票数 0

代码用于检查浏览器是否支持CSS伪类。它在JavaScript中工作得很好,但是在类型记录中会抛出错误。下面是打字本代码:

代码语言:javascript
复制
var supportsPseudo = function(pseudoClass) {
      // Get the document stylesheet
      var ss = document.styleSheets[0];

      // Create a stylesheet if one doesn't exist
      if (!ss) {
        var el = document.createElement("style");
        document.head.appendChild(el);
        ss = document.styleSheets[0];
        document.head.removeChild(el);
      }

      // Test the pseudo-class by trying to style with it
      var testPseudo = function() {
        try {
          if (!/^:/.test(pseudoClass)) {
            pseudoClass = ":" + pseudoClass;
          }
          ss.insertRule("html" + pseudoClass + "{}", 0);
          ss.deleteRule(0);
          return true;
        } catch (e) {
          return false;
        }
      };

      // Run the test
      return testPseudo();
    };
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-20 08:02:41

尝试使用这个CSSStyleSheet接口来指定ss的类型,下面是一个要引用的链接

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59818843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档