


js implements css style selector (2KB after compression)_javascript skills
I have been working on some OA front-end interfaces recently. In order to better manage the page code, I wanted to write a js selector. I found it very laborious, so I simply searched online. The hard work paid off. I found a mini css selector with good performance. Extraordinary: The following code is compressed, only 2KB.
var $=(function(){var b=/ (?:[w-\.#] ) (?:[w ?=(['"])?(?:\1|.) ?1])?|*|>/ig,g=/^ (?:[w-_] )?.([w-_] )/,f=/^(?:[w-_] )?#([w-_] )/,j=/^([ w*-_] )/,h=[null,null];function d(o,m){m=m||document;var k=/^[w-_#] $/.test(o); if(!k&&m.querySelectorAll){return c(m.querySelectorAll(o))}if(o.indexOf(",")>-1){var v=o.split(/,/g),t= [],s=0,r=v.length;for(;s
Share the original version:
/**
* "미니" 선택기 엔진
* Copyright (c) 2009 James Padolsey
* ------------- ----------------
* MIT 및 GPL 라이센스에 따라 이중 라이센스가 부여되었습니다.
* - http://www.opensource.org/licenses/mit-license.php
* - http://www.gnu.org/copyleft/gpl.html
* ---- ------------------------------------- -
* 버전: 0.01(베타)
*/
var mini = (function(){
var 스낵 = /(?:[w-\.#] ) (?:[w ?= (['"])?(?:\1|.) ?1])?|*|>/ig,
exprClassName = /^(?:[w-_] )?.([w- _] )/,
exprId = /^(?:[w-_] )?#([w-_] )/,
exprNodeName = /^([w*-_] )/,
na = [null,null];
function _find(selector, context) {
/**
* 이것은 x()를 통해 호출하는 것입니다. 이것은 x를 통해 호출하는 것입니다.
* 모든 것을 시작합니다... 모든 것을 시작합니다
*/
context = context || [w-_#] $/.test(selector);
if (!simple && context.querySelectorAll) {
return realArray(context.querySelectorAll(selector))
}
if ( selector.indexOf(',') > -1) {
var 분할 = selector.split(/,/g), ret = [], sIndex = 0, len = 분할.길이
for( ; sIndex < sIndex) {
ret = ret.concat( _find(split[sIndex], context) )
}
return Unique(ret); parts = selector.match(snack),
part = parts.pop(),
id = (part.match(exprId) || na)[1],
className = !id && (부분 .match(exprClassName) || na)[1],
nodeName = !id && (part.match(exprNodeName) || na)[1],
if (className && !nodeName) && context.getElementsByClassName) {
collection = realArray(context.getElementsByClassName(className));
} else {
collection = !id && realArray(context.getElementsByTagName(nodeName || '*'));
if (className) {
collection = filterByAttr(collection, 'className', RegExp('(^|\s)' className '(\s|$)'));
}
if (id) {
var byId = context.getElementById(id);
ID로 반환하시겠습니까?[byId]:[];
}
}
부품 반품[0] && 컬렉션[0] ? filterParents(parts, collection) : 컬렉션;
}
function realArray(c) {
/**
* 노드 컬렉션을 노드 컬렉션으로 변환
* 실제 배열 실제 배열
*/
try {
return Array.prototype.slice.call(c);
} catch(e) {
var ret = [], i = 0, len = c.length;
for (; i < len; i) {
ret[i] = c[i];
}
return ret;
}
}
function filterParents(selectorParts, collection, direct) {
/**
* 여기서 마법이 일어납니다.
* 부모가
* 장치 선택
을 준수하는지 확인합니다.*/
var parentSelector = selectorParts.pop();
if (parentSelector === '>') {
return filterParents(selectorParts, collection, true);
}
var ret = [],
r = -1,
id = (parentSelector.match(exprId) || na)[1],
className = !id && ( parentSelector.match(exprClassName) || na)[1],
nodeName = !id && (parentSelector.match(exprNodeName) || na)[1],
cIndex = -1,
node, 부모,
일치;
nodeName = nodeName && nodeName.toLowerCase();
while ( (노드 = 컬렉션[ cIndex]) ) {
parent = node.parentNode;
do {
일치 = !nodeName || nodeName === '*' || nodeName === parent.nodeName.toLowerCase();
일치 = && 일치(!id || parent.id === id);
일치 = 일치 && (!className || RegExp('(^|\s)' className '(\s|$)').test(parent.className));
if (직접 || 일치) { break; }
} while ( (parent = parent.parentNode) );
if (일치) {
ret[ r] = node;
}
}
return selectorParts[0] && ret[0] ? filterParents(selectorParts, ret) : ret;
}
var Unique = (function(){
var uid = new Date();
var data = (function(){
var n = 1;
반환 함수 (elem) {
var 캐시Index = elem[uid],
nextCacheIndex = n
if(!cacheIndex) {
elem[uid] = nextCacheIndex;
return true; }
return false;
};
return function(arr) {
/**
* 고유한 배열을 반환합니다. 고유한 배열을 반환합니다.
*/
var length = arr.length,
ret = [],
r = -1,
i = 0,
항목
for (; i item = arr[i; ];
if (data(item)) {
ret[ r] = item;
}
}
uid = 1;
return; 🎜>})();
function filterByAttr(collection, attr, regex) {
/**
* 속성을 기준으로 컬렉션을 필터링합니다.
*/
var i = -1, node, r = -1, ret = [];
while ( (node = collection[ i]) ) {
if (regex.test(node[attr])) {
ret[ r] = node;
}
}
return ret;
}
return _find;
})();
以上代码支持css风格样式写法包括:
复代码
代码如下:
div
.example
body div
div, p
div, p, .example
div p
div > p
div.example
ul .example
#title
h1#title
div #title
ul.foo > * span

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In CSS, the identifier of the id selector is "#". You can specify a specific style for the HTML element marked with a specific id attribute value. The syntax structure is "#ID value {attribute: attribute value;}". The ID attribute is unique and non-repeatable in the entire page; the ID attribute value should not start with a number. IDs starting with numbers will not work in Mozilla/Firefox browsers.

Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3. The specific code example is as follows: HTML code: <divid="container"><divclass="item"> ;First child element</div><divclass="item"&

The JavaScript selector fails because the code is not standardized. The solution is: 1. Remove the imported JS code and the ID selector method will be effective; 2. Just introduce the specified JS code before introducing "jquery.js".

In the previous article "Css Pseudo-Selector Learning - Pseudo-Element Selector Analysis", we learned about pseudo-element selectors, and today we take a closer look at pseudo-class selectors. I hope it will be helpful to everyone!

Not included. CSS selectors include: 1. Tag selector, which locates specific HTML elements through the element name of the HTML page; 2. Class selector, which locates specific HTML elements through the value of the class attribute of the HTML element; 3. ID selector, which Locate specific HTML elements through the value of the id attribute of the HTML element; 4. The wildcard selector "*" can refer to all types of tag elements, including custom elements; 5. The attribute selector uses the existing attribute name of the HTML element or attribute value to locate a specific HTML element.

In-depth analysis of is and where selectors: improving the level of CSS programming Introduction: In the process of CSS programming, selectors are an essential element. They allow us to select and style elements in an HTML document based on specific criteria. In this article, we will take a deep dive into two commonly used selectors namely: is selector and where selector. By understanding their working principles and usage scenarios, we can greatly improve the level of CSS programming. 1. is selector is selector is a very powerful choice

lxml is a powerful Python library for processing XML and HTML documents. As a parsing tool, it provides a variety of selectors to help users easily extract the required data from documents. This article will introduce the selectors supported by lxml in detail. lxml supports the following selectors: Tag selector (ElementTagSelector): Select elements by tag name. For example, select elements with a specific tag name by using <tagname>

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.
