简体   繁体   English

Javascript选择器引擎和内置函数

[英]Javascript selector engines and built in functions

I have found the following tutorial on creating a selector engine.. http://blog.insicdesigns.com/2010/04/creating-your-own-selector-engine/ 我发现了以下有关创建选择器引擎的教程。http://blog.insicdesigns.com/2010/04/creating-your-own-selector-engine/

In javascript we have functions like 在javascript中,我们具有以下功能

  • getElementById() getElementById()
  • getElementsByTageName() getElementsByTageName()
  • getElementsByName() getElementsByName()

etc,.....But for the same functionality,in their selector engine,they are doing checks like 等等.....但是对于相同的功能,在他们的选择器引擎中,他们正在执行检查

 this.nodes[i].tagName == nm.toUpperCase() 

instead of getElementsByTagName.What is the advantage of this approach?... 而不是getElementsByTagName。这种方法的优点是什么?...

Also what is the usage of assigning all nodes to a vairiable using 还有使用以下方法将所有节点分配给一个可变对象的用法

 e.getElementsByTagName('*');

this.nodes[i].tagName == nm.toUpperCase() is part of a method to filter the list of nodes by tag name.... so nothing to do with 'getting elements by their tag name' this.nodes[i].tagName == nm.toUpperCase()是通过标记名过滤节点列表的方法的一部分。...因此与“通过标记名获取元素”无关

the last point is not a real question.. you want to know reasons for "why would you select all nodes"? 最后一点不是一个真正的问题..您想知道“为什么要选择所有节点”的原因? well you are writing a sector engine.... 好吧,您正在编写一个扇区引擎。

The following line 下一行

this.nodes[i].tagName == nm.toUpperCase() 

Is within the function ofTag . ofTag的功能之ofTag It's filtering a set of nodes looking for nodes with the given name. 它过滤一组节点,以查找具有给定名称的节点。

The next line 下一行

e.getElementsByTagName('*');

Retrieves all the children/descendants under a node so you can later filter it like the following 检索节点下的所有子代/后代,以便以后可以按以下方式对其进行过滤

new DOMNodes(Selector.getAll(document)).ofTag('p').hasClass('note');  

There is an inconsistency when you get the tagName property of elements. 获取元素的tagName属性时存在不一致 Some browsers return uppercase and others lowercase. 一些浏览器返回大写字母,而其他浏览器返回小写字母。 To normalize the output of the value, you have to do one or the other before continuing further operation. 为了规范该值的输出,您必须执行另一项操作才能继续进行进一步的操作。

As for e.getElementsByTagName('*'); 至于e.getElementsByTagName('*'); , i recently answered a question where the OP wants to find ALL elements containing an attribute name which has a prefix mce_ . 我最近回答了一个问题 ,OP希望查找包含属性名称带有前缀mce_所有元素。 The only way to get such elements is to get all elements in the DOM, and inspect their attribute names. 获取此类元素的唯一方法是获取DOM中的所有元素,并检查其属性名称。

There is also a good application of this getElementsByTagName('*') and that is determining the direct child of an element. getElementsByTagName('*')也有很好的应用,它可以确定元素的直接子元素。 For instance, in a very deep DOM. 例如,在非常深的DOM中。 If I were to find certain parent elements based on an attribute and get it's children, normally you would do a recursive search from body downwards to find the parent. 如果我要基于某个属性找到某些父元素并将其作为子元素,通常您会从主体向下进行递归搜索以找到父元素。 This would take a lot of recursive operations. 这将需要大量的递归操作。 Afterwards, you determine their children. 然后,确定他们的孩子。

Another way to do it is to get all tags, determine their parent node, and if they have the parent with the attribute, they are the direct child. 另一种方法是获取所有标签,确定其父节点,如果它们的父节点具有属性,则它们是直接子节点。 This method requires no recursion, only getElementsByTagName('*') and a loop through the nodeList that was returned. 此方法不需要递归,只需要getElementsByTagName('*')和通过返回的nodeList的循环即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM