var divs = document.querySelectorAll('div');

[].forEach.call(divs, function(div) {
// do whatever
div.style.color = "red";
});


http://www.webhek.com/ditching-jquery
1,document.getElementById("foo");
2,document.getElementsByClassName('bar');
lochNess = document.querySelector(".monsters");
document.querySelectorAll(".monsters");

3,document.getElementsByTagName('span')
4,document.getElementById('foo').getElementsByTagName('span')
document.documentElement,document.head,document.body var newEl = document.createElement('div');
5,document.getElementById('foo').innerHTML="test";
textContent
6, list.classList.add("zoomed");
list.classList.remove("zoomed");
classes.toggle("hidden");
if ( elem.classList.contains('some-third-class') ) { // Check for class
console.log('yep!');
}

7,ninjaTurtles.forEach(function (entry) {
console.log(entry);
});

8,var lovesPizza = ninjaTurtles.map(function (entry) {

return entry.concat(" loves pizza!");

});
9,[].forEach.call(document.querySelectorAll('.el'), function (el) {
el.addEventListener('event', function() {

}, false);
});
10,
document.querySelector('.el').setAttribute('key', 'value');
document.querySelector('.el').getAttribute('key');
if ( elem.hasAttribute('data-example') ) { // Check data attribute
console.log('yep!');
}
11,
document.querySelector('.el').appendChild(document.createElement('div'));
var clonedEl = document.querySelector('.el').cloneNode(true);
12,
function remove(el) {
var toRemove = document.querySelector(el);

toRemove.parentNode.removeChild(toRemove);
}
13,
document.querySelector('.el').previousElementSibling;
document.querySelector('.el').nextElementSibling;
14,.
// Function to filter what's clicked and run your functions
var eventHandler = function () {

// Get the clicked element
var toggle = event.target;

// If clicked element is the one you're looking for, run your methods
if ( toggle.hasAttribute('data-example') || toggle.classList.contains('sample-class') ) {
event.preventDefault(); // Prevent default click event
someMethod( the, arguments, to, pass, in );
}

};

// Listen for all click events on the document
document.addEventListener('click', eventHandler, false);

15,
var isInViewport = function ( elem ) {
var distance = elem.getBoundingClientRect();
return (
distance.top >= 0 &&
distance.left >= 0 &&
distance.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
distance.right <= (window.innerWidth || document.documentElement.clientWidth)
);
};

var elem = document.querySelector('#some-element');
isInViewport(elem); // Boolean: returns true/false
16,
var elem = document.querySelector('#some-element');
var all = elem.childNodes;

 posted on 2016-04-19 00:02  jayruan  阅读(241)  评论(0编辑  收藏  举报