【问题标题】:Dropdown Javascript error: object doesn't support property or method 'matches'下拉 Javascript 错误:对象不支持属性或方法“匹配”
【发布时间】:2016-08-19 22:26:23
【问题描述】:

我正在使用以下 JavaScript 下拉菜单,该下拉菜单在除新版 Windows Edge 之外的所有浏览器中都能完美运行。

显示此错误:

SCRIPT438:对象不支持属性或方法“匹配”

脚本:

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

http://www.w3schools.com/howto/howto_js_dropdown.asp 获得脚本,我认为它与所有平台兼容。现在我已经实现了,但在 Edge 中遇到了问题。

【问题讨论】:

    标签: javascript jquery methods drop-down-menu multiple-matches


    【解决方案1】:

    根据http://caniuse.com/#search=matches EDGE 部分支持前缀“ms”。

    【讨论】:

      【解决方案2】:

      您似乎尝试检查单击事件是否由具有类 dropbtn 的对象触发。

      如果你使用 jQuery,你可以这样做:

      function myFunction() {
          document.getElementById("myDropdown").classList.toggle("show");
      }
      
      // Close the dropdown menu if the user clicks outside of it
      window.onclick = function(event) {
        if (!$(event.target).hasClass('dropbtn')) {
          var dropdowns = document.getElementsByClassName("dropdown-content");
          var i;
          for (i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (openDropdown.classList.contains('show')) {
              openDropdown.classList.remove('show');
            }
          }
        }
      }
      

      如果你不使用 jQuery,你可以获取 className,然后检查 dropbtn 是否是其中之一。

      function myFunction() {
          document.getElementById("myDropdown").classList.toggle("show");
      }
      
      // Close the dropdown menu if the user clicks outside of it
      window.onclick = function(event) {
        var classes = event.target.className.split(' ');
        var found = false; var i = 0;
        while (i < classes.length && !found) {
            if (classes[i]=='dropbtn') found = true;
            else ++i;
        }
        if (!found) {
          var dropdowns = document.getElementsByClassName("dropdown-content");
          var i;
          for (i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (openDropdown.classList.contains('show')) {
              openDropdown.classList.remove('show');
            }
          }
        }
      }
      

      【讨论】:

      • 非常感谢@Marc Compte 那是一个很大的帮助:-)
      【解决方案3】:

      对于跨浏览器的解决方案,请查看http://youmightnotneedjquery.com/#matches_selector

      var matches = function(el, selector) {
        return (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector).call(el, selector);
      };
      
      matches(el, '.my-class');
      

      【讨论】:

        【解决方案4】:

        正如之前提到的,IE11 部分支持它。 试试这个

        if (!Element.prototype.matches) {
        
            Element.prototype.matches = Element.prototype.msMatchesSelector;
        
        }
        

        【讨论】:

          猜你喜欢
          • 2021-05-16
          • 1970-01-01
          • 2016-05-14
          • 2013-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多