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

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

问题描述

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

  $('。this')或$('#this ')或$('div','.this')

不依赖于jQuery或使用如果jQuery可用,但如果没有,它可以没有它吗?我搜索了这个高低。也许我正在搜索不正确,因为我最接近的是:



选择没有jQuery的元素



但是,这不像我想要的那么深入,或者像我想要的一样到jQuery。我已经考虑过挖掘jQuery源代码,并将这些内容用于使用它,但我希望有人已经做到了,我只是在错误的地方看,有人知道它。



更新



这已经以几种方式回答了,谢谢你的快速回复。我用错误的方法搜索我终于来到: https://github.com/ded/qwery



然而,这里的答案完美无缺:
用于类/ id选择的jQuery的轻量级替代

解决方案

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



更新详细信息:



看起来ZeptoJS会执行以下操作。这对于 $$(document,'#myid') $$(document,'.myclass') $$(document,'div')并缓慢搜索 $$(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)
);
}


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 someone 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

解决方案

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

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

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