Lightweight alternative to jQuery for class / id selecting

前端 未结 6 1260
半阙折子戏
半阙折子戏 2020-12-31 07:42

Let\'s say I want to build a non-dependent javascript framework/script. Is there a way to utilize jQuery\'s amazing class and element selecting functionality



        
相关标签:
6条回答
  • 2020-12-31 08:14

    The answer to "I need a small JS library that..." is this site: http://microjs.com/

    specifically, you're looking for a selector engine:

    http://microjs.com/#css

    0 讨论(0)
  • 2020-12-31 08:15

    In everything but IE6 and IE7, you can use document.querySelectorAll or someElement.querySelectorAll to perform similar selection functionality.

    Update more details:

    It looks like ZeptoJS does the following. This uses quick functions for $$(document, '#myid'), $$(document, '.myclass'), $$(document, 'div') and slow searches for $$(document, 'div > .myclass')

    var classSelectorRE = /^\.([\w-]+)$/,
        idSelectorRE = /^#([\w-]+)$/,
        tagSelectorRE = /^[\w-]+$/;
    
    $$ = function(element, selector){
      var found;
      return (element === document && idSelectorRE.test(selector)) ?
        ( (found = element.getElementById(RegExp.$1)) ? [found] : [] ) :
        Array.prototype.slice.call(
          classSelectorRE.test(selector) ? element.getElementsByClassName(RegExp.$1) :
          tagSelectorRE.test(selector) ? element.getElementsByTagName(selector) :
          element.querySelectorAll(selector)
        );
    }
    
    0 讨论(0)
  • 2020-12-31 08:20

    Just try jBone, library for Events and DOM manipulation. jBone has much better performance then jQuery/Zepto, smaller size and full support for all selectors, events API's.

    0 讨论(0)
  • 2020-12-31 08:24

    MicroSelector. Even smaller and faster than Zepto, which is smaller than Sizzle, which is smaller than JQuery.

    0 讨论(0)
  • 2020-12-31 08:26

    You could do what jQuery does and use Sizzle: http://sizzlejs.com/

    0 讨论(0)
  • 2020-12-31 08:28

    Have you looked at zepto.js? You'd still be dependent on a framework, but it's much lighter weight: about 5kb instead of 31kb.

    0 讨论(0)
提交回复
热议问题