10个实用的jQuery代码片段
4. 点击后显示/隐藏更多内容
<p><a href="#how-to" class$('table').delegate('td', 'hover', function(){ $(this).toggleClass('hover');});7.? 如果rel设置为"external",在新窗口打开一个link
<a href="http://www.google.com" rel="external">Google</a>$('a[rel=external]').attr('target', '_blank');8. 添加奇数行class来区分table行(改变table奇偶行背景色以达到区分行效果)
$('tr:odd').addClass('odd');9. 检查是否div存在于页面
if ( $('#myDiv').length ) { // do something with myDiv}10. 选中/不选中所有的复选框
<div id="option1" /><label for="option1">Option 1</label> <input type="checkbox" id="option2" /><label for="option2">Option 2</label> <input type="checkbox" id="option3" /><label for="option3">Option 3</label> <input type="checkbox" id="option4" /><label for="option4">Option 4</label></div>$('.select-all').live('click', function(){ $(this).closest('.options').find('input[type=checkbox]').attr('checked', true); return false;});$('.reset-all').live('click', function(){ $(this).closest('.options').find('input[type=checkbox]').attr('checked', false); return false;});