jQuery最佳实践


Title

  1. 使用最新版本的jQuery

Title

$('#id')
$('form')
$('input') $(':hidden')
 $('[attribute=value]')

Title

$('.child', $parent)
$parent.find('.child')
$parent.children('.child')
$('#parent > .child')
$('#parent .child')
$('.child', $('#parent'))


$('a').click(function(){
    alert($(this).attr('id'));
});

$('a').click(function(){
    alert(this.id);
});

$('#top').find('p.classA');
$('#top').find('p.classB');


var $top = $('#top'),
    $classA = $top.find('p.classA'),
    $classB = $top.find('p.classB');

$('div').find('h3').eq(2).html('Hello').end().find('h3');



$('td').each(function () {
    $(this).on('click', function(){
        $(this).toggleClass('click');
    });
});

$('table').on('click', 'td', function () {
    $(this).toggleClass('click');
});



var elem = $('#elem');
elem.data(key,value);


var elem = $('#elem');
$.data(elem[0],key,value);


function doSomthing{
    doSomethingElse();
    doOneMoreThing();
}

//=============
function doSomething{
    $.trigger("DO_SOMETHING_DONE");
}

$(document).on("DO_SOMETHING_DONE", function(){
    doSomethingElse(); 
});

//=============
function doSomething(){
    var dfd = new $.Deferred();
    return dfd.promise();
}
function doSomethingElse(){
    $.when(doSomething())
        .then(//The next thing);
}

JQUERY最佳实践

By Charlee Green

JQUERY最佳实践

  • 341