高级 jQuery:让好的应用程序变成强大的应用程序
2010-01-14 00:00:00 来源:WEB开发网
图 2. Bind/Unbind 示例
清单 1 显示了以上设置的代码,这是未改进之前的原始代码:
清单 1. 未经过优化的小部件
$(document).ready(function(){
// cache this query since it's a search by CLASS
selectable = $(":checked.selectable");
// when the select/deselect all is clicked, do this function
$("#selectall").click(selectAll);
// whenever any individual checkbox is checked, change the text
// describing how many are checked
selectable.click(changeNumFilters);
// calculate how many are initially checked
changeNumFilters();
});
var selectable;
function changeNumFilters()
{
// this needs to be checked on every call
// since the length can change with every click
var size = $(":checked.selectable").length;
if (size > 0)
$("#selectedCount").html(size);
else
$("#selectedCount").html("0");
}
// handles the select/deselect of all checkboxes
function selectAll()
{
var checked = $("#selectall").attr("checked");
selectable.each(function(){
var subChecked = $(this).attr("checked");
if (subChecked != checked)
{
$(this).click();
}
});
changeNumFilters();
}
更多精彩
赞助商链接