Ruby's Louvre

每天学习一点点算法

导航

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>

posted on 2010-06-01 17:59  司徒正美  阅读(2421)  评论(3编辑  收藏  举报