Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 28th, 2012  |  syntax: None  |  size: 2.01 KB  |  views: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <html>
  2.  
  3. <style>
  4. div {
  5.   height: 40px;
  6.   margin: 40px 0;
  7.   background-color: red;
  8. }
  9. div.A {
  10.   background-color: green;
  11. }
  12. </style>
  13.  
  14. <c id=test>
  15.  <div class=A>s</div>
  16.  <br>
  17.  <div class=B>s</div>
  18.  <br>
  19.  <div class="A C">s</div>
  20. </c>
  21.  
  22. <script>
  23. //https://gist.github.com/2369850
  24. if(!Element.prototype.matchesSelector) {
  25.   Element.prototype.matchesSelector =
  26.     Element.prototype.webkitMatchesSelector ||
  27.     Element.prototype.mozMatchesSelector ||
  28.     Element.prototype.msMatchesSelector ||
  29.     Element.prototype.oMatchesSelector || function(selector) {
  30.       if(!selector)return false;
  31.       if(selector === "*")return true;
  32.  
  33.       var thisObj = this,
  34.         parent,
  35.         i,
  36.         str,
  37.         tmp,
  38.         match = false;
  39.  
  40.       if(/^[\w#\.][\w-]*$/.test(selector) || /^(\.[\w-]*)+$/.test(selector)) {
  41.         switch (selector.charAt(0)) {
  42.           case '#':
  43.             return thisObj.id === selector.slice(1);
  44.           break;
  45.           case '.':
  46.             match = true;
  47.             i = -1;
  48.             tmp = selector.slice(1).split(".");
  49.             str = " " + thisObj.className + " ";
  50.             while(tmp[++i] && match) {
  51.               match = !!~str.indexOf(" " + tmp[i] + " ");
  52.             }
  53.             return match;
  54.           break;
  55.           default:
  56.             return thisObj.tagName && thisObj.tagName.toUpperCase() === selector.toUpperCase();
  57.         }
  58.       }
  59.       parent = thisObj.parentNode;
  60.      
  61.       if(parent && parent.querySelector) {
  62.         match = parent.querySelector(selector) === thisObj;
  63.       }
  64.  
  65.       if(!match && (parent = thisObj.ownerDocument)) {
  66.         tmp = parent.querySelectorAll(selector);
  67.           for (i in tmp ) if(_hasOwnProperty(tmp, i)) {
  68.               match = tmp[i] === thisObj;
  69.               if(match)return true;
  70.           }
  71.       }
  72.         return match;
  73.     }
  74. }
  75.  
  76. function delegate(e,f){return function(c,b,a){for(a=c.target;a&&!1!==b&&a!=this;a=a.parentElement)a.matchesSelector(e)&&(b=f.call(a,c));return b}}
  77.  
  78. test.addEventListener("click", delegate(".A", function(e){console.log(this)}))
  79. </script>