用于类/id 选择的 jQuery 轻量级替代方案
假设我想构建一个不依赖的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以执行 jQuery 的操作并使用 Sizzle:http://sizzlejs.com/
You could do what jQuery does and use Sizzle: http://sizzlejs.com/
“我需要一个小型 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
在除 IE6 和 IE7 之外的所有版本中,您都可以使用
document.querySelectorAll
或someElement.querySelectorAll
来执行类似的选择功能。更新更多详细信息:
看起来ZeptoJS执行以下操作。这使用了
$$(document, '#myid')
、$$(document, '.myclass')
、$$(document, ' div')
并缓慢搜索$$(document, 'div > .myclass')
In everything but IE6 and IE7, you can use
document.querySelectorAll
orsomeElement.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')
您看过 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.
只需尝试 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.
微选择器。甚至比 Zepto 更小、更快,而 Zepto 又比 Sizzle 小,而 Sizzle 又比 JQuery 小。
MicroSelector. Even smaller and faster than Zepto, which is smaller than Sizzle, which is smaller than JQuery.