为应对HTML5的到来,对元素的寻找与匹配越显重要。FF与safari率先实现matchesSelector方法,这功能相当于jQuery的is方法。这东西对于事件代理无疑是大有裨益!

 
<!doctype html>
<html>
  <head>
    <title>matchesSelector by 司徒正美</title>
    <meta charset="utf-8"/>
    <meta content="IE=8" http-equiv="X-UA-Compatible"/>
    <meta name="keywords" content="matchesSelector by 司徒正美" />
    <meta name="description" content="matchesSelector by 司徒正美" />
  </head>
  <body>

    <div >matchesSelector!</div>
    <script type="text/javascript">
      //http://dev.w3.org/2006/webapi/selectors-api2/#matchtesting
        var el = document.getElementById("foo");
        var method = el.matchesSelector  || el.webkitMatchesSelector || el.mozMatchesSelector 
      
        if (method && method.call(el,"div")) {
          alert("此元素匹配给定选择器");
        }
   
    
    </script>
  </body>
</html>

相关文章:

  • 2022-03-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2022-01-09
  • 2022-12-23
  • 2021-09-21
  • 2021-11-17
相关资源
相似解决方案