开发者

Lightweight alternative to jQuery for class / id selecting

开发者 https://www.devze.com 2023-04-13 03:54 出处:网络
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

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

$('.this') or $('#this') or $('div', '.this')

Without being dependent on jQuery or using jQuery if it is available but if not, it works without it? I have searched this high and low. Maybe I am searching incorrectly as the closest I have gotten is this:

Selecting elements without jQuery

However, that is not as in-depth as I want or as similar as I want to jQuery. I have thought about digging through jQuery source and gutting that piece out and using it, but I hope someon开发者_如何学Ce has already done this and I am just looking in the wrong place and someone else knows about it.

Update

This has been answered in a few ways, and thank you to the quick replies. I was searching in the wrong method. I finally came on: https://github.com/ded/qwery

However this answer here does the job perfectly: Lightweight alternative to jQuery for class / id selecting


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


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


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)
    );
}


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消