AJax技术

jQuery中的100个技巧(7)

字号+ 作者:H5之家 来源:H5之家 2017-02-24 10:01 我要评论( )

input id=placeholder=/script src=/scriptscript // Define the placeholder plugin $.fn.placeholder = function(){ document.createElement( )){ // This browser already supports placeholders. ;} this .each

<input id=placeholder=/> <script src=></script> <script> // Define the placeholder plugin $.fn.placeholder = function(){ document.createElement()){ // This browser already supports placeholders. ; } this.each(function(){ var input = $(this); input.on(, function(){ )){ input.val(''); } }).on(, function(){ if(input.val() == ''){ input.val(input.attr()); } }); // Show the placeholder on load input.trigger(); }); return this; }; // And here is how to use it: $().placeholder();

 

 

 

49.使用匿名函数来产生一个独立的代码块

  定义全局变量和函数是一种代码很粗糙的行为,更好的方式是通过使用匿名函数使你的代码独立于块之中。看下面的例子:

// Isolating a block of code: (function($){ c = 1; // Define a simple plugin $.fn.count = function(){ // Increment and log the counter log(c++); return this; }; })(jQuery); // The c variable is only visible for the plugin and will keep // its value between invocations: $(document).count(); $().count().count();

 

 

50. 用extend融合对象

  当提到从多个项目到一个项目结合属性时,你最好的猜测就是扩展方法。

// Combine properties (useful in plugins). supplied = { height: 400 }; var options = $.extend({ color : , width : 200, height : 150 }, supplied); log(, options); // You can also pass more than one object log(, $.extend({a:2}, {b:3}, {c:4}) ); log(); // Cloning objects. // To clone an object, simply pass an empty one original = {a:}; var clone = $.extend({}, original); log(, clone); log(); // Extending jQuery. // You can define plugins with extend $.extend($.fn, { plugin1: function(){ log(); return this; }, plugin2: function(){ log(); return this; } }); $().plugin1().plugin2(); log(); // If you pass only one arguments to $.extend, // it will add the properties to the jQuery object $.extend({ dontDoThis : 123}); log($.dontDoThis); log(); // Deep cloning. // If you have nested objects, you will have to obj1 = { a: 1, b: 2, c: {d: 3} }; var obj2 = { c: {e: 4}, f:5}; // This won't work // $.extend(obj1, obj2); // This will $.extend(true, obj1,obj2); log(, obj1);

 

  

 

 

 持续更新中...

 

 

 

注:原创文章,如需转载,请注明出处。博客地址:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 用AJAX返回HTML片段中的JavaScript脚本

    用AJAX返回HTML片段中的JavaScript脚本

    2017-02-23 08:00

  • jQuery $.ajax .abort() 大猫 (Madao)

    jQuery $.ajax .abort() 大猫 (Madao)

    2017-02-20 09:00

  • FileUpload using Jquery Ajax and Generic Handler

    FileUpload using Jquery Ajax and Generic Handler

    2017-02-20 08:03

  • MagicSuggest dynamic ajax source

    MagicSuggest dynamic ajax source

    2017-02-19 16:00

网友点评