Skip to main content

Tedshd's Dev note

jQuery-design pattern

Table of Contents

# modulize

  • modulize selector
var module = $('body');
module.on('click', 'a', function() {
		// dosomething...
});
  • initialize
if ($('body').length) {
		// dosomething
}

# selector

id is better than tag and class

HTML

<div>
    <ul>
    	<li id="id" class="class">text</li>
    </ul>
</div>

JavaScript

$('#id').html();
$('body').find('#id');

jsperf

##bind event use on bind on parent selector

$('ul').on('click', 'li', function () {
	// dosomething
});

js Bin