Test if a browser supports a CSS selector

前端 未结 4 817
忘掉有多难
忘掉有多难 2020-12-15 09:55

Really simple: how do I most accurately test if a browser has support for a certain CSS selector?

I currently have some CSS code that makes the page a little more in

4条回答
  •  长情又很酷
    2020-12-15 10:14

    You could use querySelector:

    function testSelector(selector, node){
      var scope = document.createElement("div");
      scope.appendChild(node);
    
      try {
        return scope.querySelector(selector) !== null;
      } catch(e) { return false; }
    }
    

    You can test it like this:

    var node = document.createElement("input");
    node.type = 'checkbox';
    node.checked = 'checked';
    
    testSelector("input:checked", node); // === true
    

    See this other question for more info on querySelector.

提交回复
热议问题