轻量级替代jQuery的class / id选择 [英] Lightweight alternative to jQuery for class / id selecting

查看:96
本文介绍了轻量级替代jQuery的class / id选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想构建一个非依赖的javascript框架/脚本。有没有办法利用jQuery的惊人的类和元素选择功能

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')

不依赖于jQuery或using jQuery如果它是可用的,但如果没有,它工作没有它?我已经搜索了这个高低。也许我搜索不正确,因为我得到的最接近的是:

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:

选择没有jQuery的元素

但是,这不是我想要的深度,到jQuery。我曾经想过通过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 someone has already done this and I am just looking in the wrong place and someone else knows about it.

更新

这有几个方法已经回答,谢谢您的快速回复。我在用错误的方法搜索。我终于来了: https://github.com/ded/qwery

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

但是,这里的答案完全可以做到:
对于class / id选择,轻量级替代jQuery

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

推荐答案

IE6和IE7,您可以使用 document.querySelectorAll someElement.querySelectorAll 执行类似的选择功能。

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

更新更多详细信息:

看起来ZeptoJS执行以下操作。这使用快速函数 $$(document,'#myid') $$(document,'.myclass') $$(document,'div> .myclass') code>

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

这篇关于轻量级替代jQuery的class / id选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆