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

Element querySelector和querySelectorAll和matches的使用 #35

Open
ClarenceC opened this issue Jan 22, 2018 · 0 comments
Open

Element querySelector和querySelectorAll和matches的使用 #35

ClarenceC opened this issue Jan 22, 2018 · 0 comments

Comments

@ClarenceC
Copy link
Owner

1.querySelector

返回第一个匹配的的第一个元素,如果没有返回Null

eg:
var body = document.querySelector('body');

var myDiv = document.querySelector('#myDiv');

var selected = document.querySelector('.selected');

var img = document.body.querySelector('img .button');

2.querySelectorAll

返回匹配的NodeLists.

eg: 
var ems = document.getElementById('myDiv').querySelectorAll('em');

var strongs = document.querySelectorAll('p strong');

3.matchesSelector

Eelement类型新增的方法。接收css 选择符,如果调用元素与该选择符匹配,返回true,否则返回false
目前除IE6-IE8,Firefox/Chrome/Safari/Opera/IE 的最新版本均已实现,但方法都带上了各自的前缀.

function matchesSelector(element,selector){
  if(element.matchesSelector){
    return element.matchesSelector(selector);
  }else if(element.msMatchesSelector){
    return element.msMatchesSelector(selector);
  }else if(element.mozMatchesSelector){
    return element.mozMatchesSelector(selector);
  }else if(element.webkitMatchesSelector){
    return element.webkitMatchesSelector(selector);
  }else{
            ...
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant