标签:
$(document).ready(function() {
console.log( "ready!" );
});
document.addEventListener(‘DOMContentLoaded‘, function () {
console.log( "ready!" );
});
$(someElement).on(‘click‘,function(){
//TODO event handler logic
});
someElement.addEventListener(‘click‘,function(){
//TODO event handler logic
});
//by id
$(‘#myElement‘);
//by class name
$(‘.myElement‘);
//by tag name
$(‘div‘);
//children
$(‘#myParents‘).children();
//complex selecting
$(‘article#first p.summary‘);
//by id
document.querySelector(‘#myElement‘);
//by class name
document.querySelectorAll(‘.myElement‘);
//by tag name
document.querySelectorAll(‘div‘);
//children
$(‘#myParent‘).children();
//complex selecting
document.querySelectorAll(‘article#first p.summary‘);
$.get("ajax/text.html",function(data){
$(".result").html(data);
alert("Load was performed.");
});
jQuery的确使ajax更易用,下面是JS的实现:
var request = new XMLHttpRequest();
request.open(‘GET‘,‘ajax/test.html‘,true);
request.onload = function(e){
if (request.readyState == 4)
//check if the get was successful
if (request.status == 200){
console.log(request.responseText);
} else {
console.error(request.statusText);
}
}
//add a class
$(‘#foo‘).addClass(‘bold‘);
//remove a class
$(‘#foo‘).removeClass(‘bold‘);
//add a class
document.getElementByID(‘foo‘).className += ‘bold‘;
//remove a class
document.getElementById(‘foo‘).className = document.getElementById(‘foo‘).className.replace(/^bold$/,‘‘);
Fade In
下面的例子证明了为何jQuery这么受欢迎。实现元素的fade in效果只需要一行代码:
$(el).fadeIn();
function fadeIn(el) {
el.style.opacity = 0;
var last = +new Date();
var tick = function() {
el.style.opacity = +el.style.opacity + (new Date() - last) / 400;
last = +new Date();
if (+el.style.opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
}
};
tick();
}
fadeIn(el);
// Hiding element
$(el).hide();
// Showing element
$(el).show();
// Hiding element
el.style.display = ‘none‘;
// Showing element
el.style.display = ‘block‘;
$("#container").append("<p>more content</p>");
document.getElementById("#container").innerHTML += "<p>more content</p>";
标签:
原文地址:http://www.cnblogs.com/panshijie205/p/5858352.html