zoukankan      html  css  js  c++  java
  • jQuery选择器对应的DOM API ——选择元素

    英文原文:http://blog.garstasio.com/you-dont-need-jquery/selectors/愚人码头注:

    • 原作者的写这文章的意图是让我们抛弃jQuery,You Don’t Need jQuery!提倡我们使用原生的JavaScript,所以收集整理了jQuery语法对应的DOM API ;
    • 原作者参数的原因可以看这里:http://blog.garstasio.com/you-dont-need-jquery/why-not/ ,个人同意他的观点,简单的页面或应用,完全可以抛弃jQuery;但是出于开发效率和开发成本的考虑,我还是比较喜欢用jQuery。
    • 本人翻译或者说拷贝这篇文章的原因是:有一部分前端只会用jQuery,什么都网上找插件,甚至滥用jQuery,一点原生的JavaScript都不会写。QQ上,微博私信经常收到类似的基础问题。前端就是折腾的活,要从基础系统的学习。所以翻译了这篇文章,希望对新手和不写原生脚本的同学有一点点的帮助。

    选择元素

    有多少次你看到一个Web应用程序或库使用jQuery执行简单琐碎的元素选择?有多少次这样写:$(#myElement')? 或者这样$('.myElement')?嘘……你不需要用jQuery选择元素!这使用DOM API也很容易做到。

    1. IDs
    2. CSS Classes
    3. Tag Names
    4. Attributes
    5. Pseudo-classes
    6. Children
    7. Descendants
    8. Exclusion Selectors
    9. Multiple Selectors
    10. See a Pattern?
    11. Filling in the Gaps
    12. Next in this Series

    By ID

    jQuery

    1. // returns a jQuery obj w/ 0-1 elements
    2. $('#myElement');

    DOM API

    1. // IE 5.5+
    2. document.getElementById('myElement');

    …或者…

    1. // IE 8+
    2. document.querySelector('#myElement');

    这两种方法返回一个Element(元素)。 需要注意的是使用 getElementById比使用querySelector更高效

    请问jQuery的语法提供任何好处吗?我没有看到一个。你呢?

    By CSS Class

    jQuery

    1. // returns a jQuery obj w/ all matching elements
    2. $('.myElement');

    DOM API

    1. // IE 9+
    2. document.getElementsByClassName('myElement');

    …或者…

    1. // IE 8+
    2. document.querySelectorAll('.myElement');

    第一个方法返回的HTMLCollection,并且效率最高的是第二个方法。 querySelectorAll总是返回一个NodeList(节点列表)

    同样,这里真的很简单的东西。为什么要使用jQuery?

    By Tag Name

    举个例子,选择页面上所有的<div>元素:

    jQuery

    1. $('div');

    DOM API

    1. // IE 5.5+
    2. document.getElementsByTagName('div');

    …或者…

    1. // IE 8+
    2. document.querySelectorAll('div');

    正如预期的那样,querySelectorAll(返回 NodeList)比getElementsByTagName(返回HTMLCollection)效率低。

    By Attribute(属性)

    选择所有”data-foo-bar”值为”someval”的元素:

    jQuery

    1. $('[data-foo-bar="someval"]');

    DOM API

    1. // IE 8+
    2. document.querySelectorAll('[data-foo-bar="someval"]');

    DOM API和jQuery语法非常相似。

    By Pseudo-class(伪类)

    选择所有在指定表单中的当前无效(:invalid 伪类)字段。假设我们的表单 ID为”myForm”。

    jQuery

    1. $('#myForm :invalid');

    DOM API

    1. // IE 8+
    2. document.querySelectorAll('#myForm :invalid');

    Children(子元素)

    选择一个特定元素的所有子元素。 假设我们的特定元素 ID为 “myParent”。

    jQuery

    1. $('#myParent').children();

    DOM API

    1. // IE 5.5+
    2. // NOTE: This will include comment and text nodes as well.
    3. document.getElementById('myParent').childNodes;

    …或者…

    1. // IE 9+ (ignores comment & text nodes).
    2. document.getElementById('myParent').children;

    但是,如果我们只想找到特定的子元素呢?比如,有 “ng-click”属性的子元素?

    jQuery

    1. $('#myParent').children('[ng-click]');

    …或…

    1. $('#myParent > [ng-click]');

    DOM API

    1. // IE 8+
    2. document.querySelector('#myParent > [ng-click]');

    Descendants(后代元素)

    找到#myParent下面所有”a”元素。

    jQuery

    1. $('#myParent A');

    DOM API

    1. // IE 8+
    2. document.querySelectorAll('#myParent A');

    Excluding Elements(排除元素)

    选择所有<div>元素,排除那些有”ignore”样式类 <div>元素。

    jQuery

    1. $('DIV').not('.ignore');

    …或者…

    1. $('DIV:not(.ignore)');

    DOM API

    1. // IE 9+
    2. document.querySelectorAll('DIV:not(.ignore)');

    Multiple Selectors(多重选择)

    选择所有<div>,<a><script>元素。

    jQuery

    1. $('DIV, A, SCRIPT');

    DOM API

    1. // IE 8+
    2. document.querySelectorAll('DIV, A, SCRIPT');

    See a Pattern?

    如果我们专注于选择器的支持,并且不需要处理IE8以下的浏览器,我们只需用这个替代jQuery:

    1. window.$ = function(selector) {
    2. var selectorType = 'querySelectorAll';
    3.  
    4. if (selector.indexOf('#') === 0) {
    5. selectorType = 'getElementById';
    6. selector = selector.substr(1, selector.length);
    7. }
    8.  
    9. return document[selectorType](selector);
    10. };

    But I Want More!

    对于绝大多数项目中,选择器支持到Web API就足够了。但是,如果你不幸需要支持IE7?在这种情况下,你可能需要一些第三方的代码来提供一些帮助。

    当然,你仅仅需要引入jQuery,但当你只需要支持现在先进的选择器时,为什么用这么大的代码库呢?相反,尝试一下micro-library(微小的库)完全专注于元素选择。考虑,Sizzle,这恰好是jQuery使用的选择库。Selectivizr是另一种非常小的选择库,在很老的浏览器上也能支持CSS3选择器。

  • 相关阅读:
    Hadoop 最讨厌的报错:运行VirtualBox提示0x00000000错误“0x00000000指令引用的0x00000000内存该内存不能为written?
    linux下面/usr/local和opt目录有何区别
    Hadoop是不是必须在linux上运行?(根本原因是操作系统Linux的权限开放优势)
    Hadoop主要配置文件的作用
    RPC模式
    Hadoop安装最后一步~Hadoop伪分布式配置
    word 使用中遇到的小细节(按空格键后面字不见;从编译器粘贴的代码出现乱码,word标题内容折叠效果实现)
    在VS Code下配置Julia
    使用pandas读取csv文件和写入文件
    ModuleNotFoundError: No module named 'sklearn.cross_validation'
  • 原文地址:https://www.cnblogs.com/zhishaofei/p/4171232.html
Copyright © 2011-2022 走看看