selector API level 2

为应对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 id="foo">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>

你可能感兴趣的:(selector)