Skip to content

Latest commit

 

History

History
67 lines (57 loc) · 1.01 KB

jquery.md

File metadata and controls

67 lines (57 loc) · 1.01 KB

DOM

:first, :last

<ul id="menu">
	<li>Item 1</li>
	<li>Item 2</li>
	<li>Item 3</li>
</ul>
// jQuery
var firstItem = $('#menu li:first')[0];
var lastItem = $('#menu li:last')[0];

// closure
var menuLi = goog.dom.getElementsByTagNameAndClass(
	'li',
	undefined,
	goog.dom.getElement('menu')
);

var firstItem = menuLi[0],
	lastItem =  menuLi[menuLi.length - 1];

.prepend()

<ul id="menu">
	<li>Item 1</li>
	<li>Item 2</li>
	<li>Item 3</li>
</ul>
// jQuery
$('#menu').prepend('<li>Item 0</li>');

// closure
var menu = goog.dom.getElement('menu');
var newItem = goog.dom.createDom('li', undefined, 'Item 0');
goog.dom.insertChildAt(menu, newItem, 0);

Event

Click handler

<div id="container" style="width: 100px; height: 100px;"></div>
// jQuery
$('#container').click(function(e) {
	// ...
});

// closure
goog.events.listen(
	goog.dom.getElement('container'),
	goog.events.EventType.CLICK,
	function(e) {
		// ...
	}
);