рдПрдХ рдмрд╛рд░ рдлрд┐рд░ рд╕рд╣рдХрд░реНрдорд┐рдпреЛрдВ рдХреЗ рдХреЛрдб рдЦреЛрд▓рдиреЗ рдФрд░ рднрдпрднреАрдд рд╣реЛрдиреЗ рдкрд░, рдореИрдВрдиреЗ рдЗрд╕ рд▓реЗрдЦ рдХреЛ рд▓рд┐рдЦрдиреЗ рдХрд╛ рдлреИрд╕рд▓рд╛ рдХрд┐рдпрд╛ред рдореБрдЭреЗ рдЙрдореНрдореАрдж рд╣реИ рдХрд┐ рдпрд╣ рдХрд┐рд╕реА рдХреЗ рд▓рд┐рдП рдЙрдкрдпреЛрдЧреА рд╣реЛрдЧрд╛, рдЙрд╕реА рд╕рдордп, рдФрд░ рдореЗрд░реЗ рд▓рд┐рдП рд╢реБрд░реБрдЖрддреА рд▓реЛрдЧреЛрдВ рдХреЛ рдпрд╣ рд╕рдордЭрд╛рдирд╛ рдЖрд╕рд╛рди рд╣реЛрдЧрд╛ рдХрд┐ рдЙрдирдХреЗ рдХреЛрдб рдореЗрдВ рдХреНрдпрд╛ рдЧрд▓рдд рд╣реИ, рдмрд╕ рдЗрд╕ рд▓реЗрдЦ рдХрд╛ рд▓рд┐рдВрдХ рдлреЗрдВрдХрдХрд░ред
рдмреЗрд╢рдХ, рдРрд╕реА рдЪреАрдЬреЛрдВ рдХреА рд╕рдВрдЦреНрдпрд╛ рдмрд╣реБрдд, рдмрд╣реБрдд рдмрдбрд╝реА рд╣реИ, рдЗрд╕рд▓рд┐рдП рд▓реЗрдЦ рдореЗрдВ рдореИрдВ рдЦреБрдж рдХреЛ рдХреЗрд╡рд▓ рдХреБрдЫ рддрдХ рд╣реА рд╕реАрдорд┐рдд рдХрд░реВрдВрдЧрд╛ред
1. рдХреЛрдб рдореЗрдВ рд▓рдЧрд╛рддрд╛рд░
рдпрд╣ рд╕рдорд╕реНрдпрд╛ рди рдХреЗрд╡рд▓ рдЬрд╛рд╡рд╛рд╕реНрдХреНрд░рд┐рдкреНрдЯ, рдмрд▓реНрдХрд┐ рд╕рдордЧреНрд░ рд░реВрдк рд╕реЗ рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рдХреА рдЪрд┐рдВрддрд╛ рдХрд░рддреА рд╣реИред рдПрдХ рдЙрджрд╛рд╣рд░рдг рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВ:
$elem.on('keydown', function(e) {
if (e.keyCode == 27) {
//...
}
});
27? , тАФ ESC. , , , , , .
, ESC, , , KEY_ESC = 27
2.
(, , ..), - . (, ajax). :
var id = $(this).attr('id').substring(8);
тАФ 8. html ..
( ):
var last_id = $('#answer_pid' + id + ' li:first div').attr('id').substr(7);
, js .
:
<div class="comment" id="comment_123"></div>
var id = $(this).attr('id').substring("comment_".length);
( ), js html.
data-* ,
<div class="comment" data-id="123"></div>
:
var id = $(this).attr('data-id');
var id = $(this).data('id');
( attr data )
3. $.post
тАФ jquery ajax тАФ $.ajax. shorthand , $.get, $.load, $.post .. , ( , json, post ), $.ajax.
shorthand , :
:
1.
$.post(url, data, function(data) {
data = $.parseJSON(data);
//...
});
2. try catch
$.post(url, data, function(data) {
try {
data = $.parseJSON(data);
} catch (e) {
return;
}
//...
});
3. , $.post dataType ( , success ).
$.post(url, data, function(data) {
//...
}, 'json');
. - , 5 , , .
$.post , :
$.post(url, data, function(data) {
//...
}, 'json').error(function() {
///
});
тАФ . тАФ , ajax , :
$.ajaxSetup({
error: function() {
// ,
}
});
$.post. тАФ $.post ( dataType ). $.ajax. .
$.ajax({
type: "POST"
url: url,
data: data,
dataType: "json",
success: function(data) {
//
},
error: function() {
//
}
});
4.
(, ┬л ┬╗). :
$('.comment a.delete').click(function(){
//
});
тАФ ( ). , ( ):
$('.comment a.delete').unbind('click').click(function() {
//
});
: jQuery 1.7 on, , .
:
$('body').on('click', 'a.external', function(e) {
// external
});
, .
, . :
$('body').on('mousemove', selector, function() {
//
});
5. Namespaced events
, namespaced events jQuery 1.2 тАФ ( ).
:
$('a').on('click', function() {
// 1
});
$('a').on('click', function() {
// 2
});
, . тАФ $('a').off('click') . namespaced events. :
$('a').on('click.namespace1', function() {
// 1
});
$('a').on('click.namespace2', function() {
// 2
});
$('a').off('click.namespace2');
namespaced events :
docs.jquery.com/Namespaced_Events, . .