20個熱門jQuery的提示和技巧

來源:互聯網
上載者:User

標籤:class   code   java   ext   color   com   

以下是一些非常有用的jQuery提示和所有jQuery的開發技巧。 

1。最佳化效能複雜的選擇查詢DOM中的一個子集,使用複雜的選擇時,大幅提高了效能:
var subset = $("");$("input[value^=‘‘]", subset);
2。設定上下文和提高效能
jQuery核心功能上,指定上下文參數。指定context參數,允許從DOM中更深的分支,而不是從DOM根,jQuery來啟動。鑒於一個足夠大的DOM,指定上下文參數轉化為效能提升。
$("input:radio", document.forms[0]);

3。現場事件處理常式

設定任何一個選擇的元素相匹配的事件處理常式,即使它被添加後的初始頁面載入到DOM:
$(‘button.someClass‘).live(‘click‘, someFunction);

這允許你通過Ajax載入內容,或通過JavaScript添加事件處理常式的成立為這些元素正確自動。 同樣,要停止現場事件處理:
4。檢查索引jQuery有索引,但它是一種痛苦,使用你所需要的元素的列表,並通過元素你想要的指標

var index = e.g $(‘#ul>li‘).index( liDomObject );
以下是比較容易:$("ul > li").click(function (){    var index = $(this).prevAll().length;});如果你想知道在一個集合元素的索引,例如在一個無序列表的清單項目:5。使用jQuery的資料的方法jQuery的資料()方法是有用的,不為人所熟知。它可以讓你無需修改DOM綁定資料到DOM元素。$("ul > li").click(function (){    var index = $(this).prevAll().length;});6。刪除一個元素的淡出效果基本show效果結合多個jQuery效果,動畫和刪除DOM元素。

$("#myButton").click(function() {         $("#myDiv").fadeTo("slow", 0.01, function(){ //fade             $(this).slideUp("slow", function() { //slide up                 $(this).remove(); //then remove from the DOM             });         });});7。檢查是否存在一個元素
if ($("#someDiv").length) {    //hooray!!! it exists...}

使用下面的程式碼片段來檢查一個元素是否存在與否。

8。添加到DOM中動態建立的元素

使用下面的程式碼片段來動態建立一個DIV,並添加到DOM。var newDiv = $(‘<div></div>‘);

newDiv.attr("id","myNewDiv").appendTo("body");

9。分行符號和chainability的

而不是做:

 $("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‘);});

11。複製在jQuery對象

使用clone()方法。jQuery的方法複製任何JavaScript中的DOM對象。

// Clone the DIVvar cloned = $(‘#somediv‘).clone();
jQuery的clone()方法不複製一個JavaScript對象。複製JavaScript對象,使用下面的代碼。
// Shallow copyvar newObject = jQuery.extend({}, oldObject); // Deep copyvar newObject = jQuery.extend(true, {}, oldObject);

12。測試,如果事情是隱藏使用jQuery

我們使用。隱藏()。在jQuery的show()方法來改變元素的知名度。使用以下代碼來檢查一個元素是否是可見或不可見。if($(element).is(":visible") == "true") {       //The element is Visible}
13。另一種方式的檔案準備就緒

//Instead of$(document).ready(function() {    //document ready});//Use$(function(){    //document ready});

14。選擇一個元素。(期),在它的ID

在選擇使用反斜線來選擇的元素,在它的ID期間。
$("#Address\\.Street").text("Enter this field");

15。計數的直接子項目

如果你要計算所有的div#foo的元素存在於
<div id="foo">  <div id="bar"></div>  <div id="baz">    <div id="biz">  </div>  <span><span></div> //jQuery code to count child elements$("#foo > div").size()
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();
18。擷取父div使用最接近

如果你想找到的封裝紙DIV元素(不論該DIV的ID),然後你會想這個jQuery選擇:
$("#searchBox").closest("div");

19。禁用按右鍵操作功能表

還有許多JavaScript片段禁用右鍵
單擊操作功能表,但jQuery讓事情容易多了:

$(document).ready(function(){    $(document).bind("contextmenu",function(e){        return false;    });

});

20。擷取滑鼠游標的X和Y軸

這個指令碼會顯示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);});

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.