Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

简写 document.querySelector() #56

Closed
Dream4ever opened this issue Nov 28, 2018 · 0 comments
Closed

简写 document.querySelector() #56

Dream4ever opened this issue Nov 28, 2018 · 0 comments
Labels
JS Javascript

Comments

@Dream4ever
Copy link
Owner

Dream4ever commented Nov 28, 2018

需求描述

由于良好的兼容性,在自己目前的业务代码中,到处都会用到 document.querySelector() 以及 document.querySelectorAll() 这两个方法。但是总是要手写这两个方法的话就太长了、太花时间了。那么怎样能够简化输入呢?

方案调研

用关键字 js short for document.querySelector Google,第一个链接是 Stack Overflow 上的回答,就是自己想要的结果。

参考资料

关键代码

var $ = document.querySelector.bind(document);
var $$ = document.querySelectorAll.bind(document);

上面这两行代码,是兼容性最好的版本,直接拿来用即可。

@Dream4ever Dream4ever changed the title 简写 document.querySelector 简写 document.querySelector() Nov 28, 2018
@Dream4ever Dream4ever added the JS Javascript label Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JS Javascript
Projects
None yet
Development

No branches or pull requests

1 participant