jQuery Tips and Tricks
Syntax
Data Storage
Optimization
Miscellaneous
This one goes out to Kobi.
Consider the following snippet of code:
// hide all elements which contains the text "abc"
$("p").each(function ()
{
var that = $(this);
if (that.text().indexOf("abc") > -1) that.hide();
});
Here's a shorthand... which is about twice as fast:
$("p.value:contains('abc')").hide();
Add a selector for elements above the fold
As a jQuery selector plugin
$.extend($.expr[':'], {
"aboveFold": function(a, i, m) {
var container = m[3],
var fold;
if (typeof container === "undefined") {
fold = $(window).height() + $(window).scrollTop();
} else {
if ($(container).length == 0 || $(container).offset().top == null) return false;
fold = $(container).offset().top + $(container).height();
}
return fold >= $(a).offset().top;
}
});
Usage:
$("p:aboveFold").css({color:"red"});
Thx to scottymac
(This is a shamless plug)
Instead of writing repetitive form handling code, you can try out this plugin I wrote that adds to the fluent API of jQuery by adding form related methods:
// elementExists is also added
if ($("#someid").elementExists())
alert("found it");
// Select box related
$("#mydropdown").isDropDownList();
// Can be any of the items from a list of radio boxes - it will use the name
$("#randomradioboxitem").isRadioBox("myvalue");
$("#radioboxitem").isSelected("myvalue");
// The value of the item selected. For multiple selects it's the first value
$("#radioboxitem").selectedValue();
// Various, others include password, hidden. Buttons also
$("#mytextbox").isTextBox();
$("#mycheck").isCheckBox();
$("#multi-select").isSelected("one", "two", "three");
// Returns the 'type' property or 'select-one' 'select-multiple'
var fieldType = $("#someid").formElementType();
链接地址: http://www.djcxy.com/p/95402.html
上一篇: Jquery手风琴关闭然后打开
下一篇: jQuery提示和技巧