用于类/id 选择的 jQuery 轻量级替代方案

发布于 2024-12-09 07:48:25 字数 854 浏览 0 评论 0原文

假设我想构建一个不依赖的 javascript 框架/脚本。有没有一种方法可以利用 jQuery 令人惊叹的类和元素选择功能,

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

而不依赖于 jQuery 或使用 jQuery(如果可用),但如果不可用,则无需使用它?我已经搜索了这么高和低。也许我搜索不正确,因为我得到的最接近的是:

Selecting elements without jQuery

然而,这并不像我想要的那么深入,也不像我想要的 jQuery 那样相似。我曾考虑过挖掘 jQuery 源代码并删除该部分并使用它,但我希望有人已经这样做了,而我只是在错误的地方查找,而其他人知道它。

更新

这个问题已通过多种方式得到解答,感谢您的快速回复。我正在以错误的方法进行搜索。我终于来了: https://github.com/ded/qwery

但是这里的答案确实工作完美: 用于类/id选择的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')

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

桃扇骨 2024-12-16 07:48:25

您可以执行 jQuery 的操作并使用 Sizzle:http://sizzlejs.com/

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

一身仙ぐ女味 2024-12-16 07:48:25

“我需要一个小型 JS 库......”的答案是这个网站:http://microjs.com/

具体来说,你'正在寻找选择器引擎:

http://microjs.com/#css

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

像你 2024-12-16 07:48:25

在除 IE6 和 IE7 之外的所有版本中,您都可以使用 document.querySelectorAllsomeElement.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)
    );
}

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)
    );
}
夏花。依旧 2024-12-16 07:48:25

您看过 zepto.js 吗?您仍然依赖于框架,但它的重量要轻得多:大约 5kb,而不是 31kb。

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.

情深缘浅 2024-12-16 07:48:25

只需尝试 jBone,用于事件和 DOM 操作的库。 jBone 的性能比 jQuery/Zepto 好得多,尺寸更小,并且完全支持所有选择器、事件 API。

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.

夜未央樱花落 2024-12-16 07:48:25

微选择器。甚至比 Zepto 更小、更快,而 Zepto 又比 Sizzle 小,而 Sizzle 又比 JQuery 小。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文