标签:class code java ext color com
以下是一些非常有用的jQuery提示和所有jQuery的开发技巧。
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");
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");
还有许多JavaScript片段禁用右键
单击上下文菜单,但jQuery让事情容易多了:
$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return false; });});
这个脚本会显示X和Y值 - 鼠标指针的坐标。
$().mousemove(function(e){ //display the x and y axis values inside the P element $(‘p‘).html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);});20个热门jQuery的提示和技巧,布布扣,bubuko.com
标签:class code java ext color com
原文地址:http://www.cnblogs.com/axl234/p/3785396.html