Edwin Martin
Fronteers side-session, Kings of Code, June 29th 2009
<p>Time is <span class='date'></span></p>
.highlight {
font-weight: bold;
color: orange;
}
$(function() {
$('p span.date').addClass('highlight')
.html(new Date().toLocaleTimeString());
});
Time is
<div id="larch"></div>
$(function() {
$('#larch')
.append('<h3>The Larch</h3>')
.find('h3')
.css('color', 'blue')
.end()
.append('<p>How to Recognise Different Types of Trees From Quite a Long Way Away.</p>')
.find('p')
.css('color', 'green');
});
jQuery.fn.pluginName = function() {
return this.each(function() {
// Do something with $(this)
}
};
$('p').pluginName();
<ul id="larch-trees">
<li>Himalayan Larch</li>
<li>Japanese Larch</li>
<li>Yunnan Larch</li>
</ul>
#larch-trees li {
width: 170px;
background-color: pink;
padding: 3px;
list-style-type: none;
margin-bottom: 2px;
}
jQuery.fn.animatedSelector = function() {
return this.each(function() {
$(this).toggle(
function() {
$(this).animate({width: 250}, 'fast');
},
function() {
$(this).animate({width: 170}, 'fast');
}
);
});
}
$(function() {
$('#larch-trees li').animatedSelector();
})
Miscelaneous (CSS3) pseudo-selectors: :first, :last, :not(selector), :even, :odd, :eq(index)...
$(function() {
$.expr[':'].findStar = function(elem) {
return $(elem).html().indexOf('(*)') != -1;
};
});
$('#larix tr:findStar td').addClass('highlight')
Larix decidua | European Larch | (*) |
Larix griffithii | Himalayan Larch | |
Larix himalaica | Langtang Larch | |
Larix kaempferi | Japanese Larch | (*) |
Larix laricina | Tamarack Larch | |
Larix mastersiana | Masters' Larch | |
Larix potaninii | Chinese Larch | |
Larix principis-rupprechtii | Prince Rupprecht's Larch | (*) |
Larix speciosa | Yunnan Larch |
Species marked with an asterisk (*) are Northern Eurasian, short-bracted
Not live:
$(function() {
$('#live1 li').bind('click', function() {
$(this).text(parseInt($(this).text())+1);
});
$('#more1').click(function() {
$('#live').append('<li>1</li>');
});
});
Live:
$(function() {
$('#live2 li').live('click', function() {
$(this).text($(this).text()*1+1);
});
$('#more2').click(function() {
$('#live2').append('<li>1</li>');
});
});
animate( params, [duration], [easing], [callback] )
jQuery.easing.myEasingFunction = function (x, t, b, c, d) {
return
};
$('#block').animate( {width: 500}, 'slow', 'myEasingFunction' )
Predefined functions in jQuery UI's effects.core.js.
var title = $('rss channel title', xmlDocument).text();
$(function() {
$('#feed').renderFeed('feed.xml');
});
jQuery.fn.renderFeed = function(location) {
return this.each(function() {
var el = $(this);
$.get(location, function(xml) {
var ul = $('<ul>');
$('rss channel item', xml).each(function() {
var a = $('<a>')
.attr('href', $('link', this).text())
.text($('title', this).text());
ul.append($('<li>').append(a));
});
el.append(ul);
});
});
};
<ul id="larch-trees2">
<li>Himalayan Larch</li>
<li>Japanese Larch</li>
<li>Yunnan Larch</li>
</ul>
#larch-trees2 li {
width: 170px;
background-color: pink;
padding: 3px;
list-style-type: none;
margin-bottom: 2px;
}
#larch-trees2 li.selected {
width: 250px;
background-color: red;
}
jQuery.fn.animatedSelector2 = function() {
return this.each(function() {
$(this).toggle(
function() {
$(this).addClass('selected', 'fast');
},
function() {
$(this).removeClass('selected', 'fast');
}
);
});
}
$(function() {
$('#larch-trees2 li').animatedSelector2();
})
http://jquery.com/
Edwin Martin <edwin@bitstorm.org>