jQuery Defining Custom Functions
I'm trying to a define my own function in jQuery to condense my js file, but I seem to be unable to shorten these two lines. Below is:
*My function
*What it looks like with the function
*What the original code looks like without the function
var AddContent = function(content) {
$(#log li:last).remove();
$(#log).append(content);
};
$('.object').click(function(){
AddContent('<li>content1</li>');
});
$('.object').click(function(){
$('#log li:last').remove();
$('#log').append('<li>content1</li>');
});
What am I doing wrong? Help!开发者_如何学运维
In your new function AddContent
-- you need to put quote around the selectors.
I.e.
var AddContent = function(content) {
$(#log li:last).remove();
$(#log).append(content);
};
Should be
var AddContent = function(content) {
$('#log li:last').remove();
$('#log').append(content);
};
精彩评论