var subset = $("");
$("input[value^='']", subset);
jQuery核心功能上,指定上下文参数。指定context参数,允许从DOM中更深的分支,而不是从DOM根,jQuery来启动。鉴于一个足够大的DOM,指定上下文参数转化为性能提升。
$("input:radio", document.forms[0]);
$('button.someClass').live('click', someFunction);
var index = e.g $('#ul>li').index( liDomObject );
以下是比较容易:$("ul > li").click(function ()
{
var index = $(this).prevAll().length;
});
$("ul > li").click(function ()
{
var index = $(this).prevAll().length;
});
$("#myButton").click(function() {
$("#myDiv").fadeTo("slow", 0.01, function(){ //fade
$(this).slideUp("slow", function() { //slide up
$(this).remove(); //then remove from the DOM
});
});
});
if ($("#someDiv").length) {
//hooray!!! it exists...
}
var newDiv = $('<div></div>');
newDiv.attr("id","myNewDiv").appendTo("body");
$("a").hide().addClass().fadeIn().hide();
像这样可以增加可读性:
$("a")
.hide()
.addClass()
.fadeIn()
.hide();
10。创建自定义选择
$.extend($.expr[':'], {
over100pixels: function(a) {
return $(a).height() > 100;
}
});
$('.box:over100pixels').click(function() {
alert('The element you clicked is over 100 pixels high');
});
// Clone the DIVvar cloned = $('#somediv').clone();
jQuery的clone()方法不克隆一个JavaScript对象。克隆JavaScript对象,使用下面的代码。
// Shallow copyvar newObject = jQuery.extend({}, oldObject);
// Deep copyvar newObject = jQuery.extend(true, {}, oldObject);
if($(element).is(":visible") == "true") {
//The element is Visible
}
13。另一种方式的文件准备就绪
//Instead of$(document).ready(function() {
//document ready
});//Use$(function(){
//document ready
});
$("#Address\\.Street").text("Enter this field");
16。做一个“闪存”的元素
jQuery.fn.flash = function( color, duration )
{ var current = this.css( 'color' );
this.animate( { color: 'rgb(' + color + ')' }, duration / 2 );
this.animate( { color: current }, duration / 2 );
}//Then use the above function as:$( '#importantElement' ).flash( '255,0,0', 1000 );
17。中心元素在屏幕上
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}//Use the above function as:$(element).center();$("#searchBox").closest("div");
单击上下文菜单,但jQuery让事情容易多了:
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});$().mousemove(function(e){
//display the x and y axis values inside the P element
$('p').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
});
996



被折叠的 条评论
为什么被折叠?



