//Where possible, pre-fix your class names with a tag name
//so that jQuery doesn't have to spend more time searching
//for the element you're after. Also remember that anything
//you can do to be more specific about where the element is
//on your page will cut down on execution/search times
var in_stock = $('#shopping_cart_items input.is_in_stock');
Item X
Item Y
Item Z
6. 如何正確使用ToggleClass複製程式碼
程式碼如下:
//Toggle class allows you to add oror remove
//from an element depending on the presence of that
//class. Where some developers would use:
a.hasClass('blueButton') ? a.removeClass('blueButton') : a.addClass('blueButton');
//toggleClass allows you to easily do this usinga.toggleClass('blueButton');
//This precision-based approached can beuse ful can beuse >
//lots of similar input elements which have different types
var elements = $('#someid input[type=sometype][value=somevalue]').get();
16. 設定任何符合一個選擇器的事件處理程序 [code]$('button.someClass').live('click', someFunction);
//Note that in jQuery 1.4.2, the delegate and undelegate options have been
//introduced to replace live as they offer better support for context
//For example, in terms of a table where before you would use..
//
.live() $("table").each(function(){ $("td", this).live("hover", function(){ $(this).toggleClass("hover"); }) ; });
//Now use..
$("table").delegate("td", "hover", function(){ $(this).toggleClass("hover"); });
$.expr[':'].mycustomselector = function(element, index, meta, stack){
// element- is a DOM element
// index - the current loop index in stack
// meta - meta data about your selector
// stack - stack of all elements to loop
// Return true to include current element
// Return false to explude current element
};
// Custom Selector usage:
//This snippet will show you how to keep >
//in a text input field for when a user hasn't entered in
//a value to replace it
swap_val = [];
$(".swap").each(function(i){ swap_val[i] = $(this).val();
$(this).focusin(function(){ if ($(this).val() == swap_val[i]) { $(this).val(""); } }).focusout(function(){ if ($.trim($(this).val()) == "") { $(this).val(swap_val[i]); } }); });
//Here's how we used to do it in 1.3.2
setTimeout(function() { $('.mydiv').hide('blind', {}, 500) }, 5000);
//And here's how you can do it with 1.4 using the delay() feature (this is a lot like sleep)
$(".mydiv").delay(5000).hide('blind', {}, 500);