{"id":2600,"date":"2013-12-18T08:38:48","date_gmt":"2013-12-18T00:38:48","guid":{"rendered":"https:\/\/sdeno.com\/?p=2600"},"modified":"2013-12-16T00:54:36","modified_gmt":"2013-12-15T16:54:36","slug":"%e7%bb%8f%e9%aa%8c%e5%88%86%e4%ba%ab%ef%bc%9a10%e4%b8%aa%e7%ae%80%e5%8d%95%e5%ae%9e%e7%94%a8%e7%9a%84jquery%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=2600","title":{"rendered":"\u7ecf\u9a8c\u5206\u4eab\uff1a10\u4e2a\u7b80\u5355\u5b9e\u7528\u7684jQuery\u6280\u5de7"},"content":{"rendered":"<p>\u5c3d\u7ba1\u5404\u79cd JavaScirpt \u6846\u67b6\u548c\u5e93\u5c42\u51fa\u4e0d\u7a77\uff0cjQuery \u4ecd\u7136\u662f Web \u524d\u7aef\u5f00\u53d1\u4e2d\u6700\u5e38\u7528\u7684\u5de5\u5177\u5e93\u3002\u4eca\u5929\uff0c\u5411\u5927\u5bb6\u5206\u4eab\u6211\u89c9\u5f97\u5728\u7f51\u7ad9\u5f00\u53d1\u4e2d10\u4e2a\u7b80\u5355\u5b9e\u7528\u7684 jQuery \u4ee3\u7801\u7247\u6bb5\u3002<\/p>\n<p><strong>\u5e73\u6ed1\u6eda\u52a8\u5230\u951a\u70b9<\/strong><\/p>\n<p>[code]\/\/ HTML:<br \/>\n\/\/ &lt;h1 id=&#8221;anchor&#8221;&gt;Lorem Ipsum&lt;\/h1&gt;<br \/>\n\/\/ &lt;p&gt;&lt;a href=&#8221;#anchor&#8221; class=&#8221;topLink&#8221;&gt;Back to Top&lt;\/a&gt;&lt;\/p&gt;<\/p>\n<p>$(document).ready(function() {<br \/>\n$(&#8220;a.topLink&#8221;).click(function() {<br \/>\n$(&#8220;html, body&#8221;).animate({<br \/>\nscrollTop: $($(this).attr(&#8220;href&#8221;)).offset().top + &#8220;px&#8221;<br \/>\n}, {<br \/>\nduration: 500,<br \/>\neasing: &#8220;swing&#8221;<br \/>\n});<br \/>\nreturn false;<br \/>\n});<br \/>\n});[\/code]<br \/>\n<strong>\u7f29\u653e\u56fe\u7247<\/strong><br \/>\n\u867d\u7136\u56fe\u7247\u5e94\u8be5\u5728\u670d\u52a1\u5668\u7aef\u7f29\u653e\uff0c\u4e0d\u8fc7\u5982\u679c\u670d\u52a1\u5668\u7aef\u672a\u505a\u7f29\u653e\uff0c\u9700\u8981\u518d\u5ba2\u6237\u7aef\u7f29\u653e\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e2a\u65b9\u4fbf\u7684\u4ee3\u7801\u7247\u6bb5\uff1a<\/p>\n<p>[code]$(window).bind(&#8220;load&#8221;, function() {<br \/>\n\/\/ IMAGE RESIZE<br \/>\n$(&#8216;#product_cat_list img&#8217;).each(function() {<br \/>\nvar maxWidth = 120;<br \/>\nvar maxHeight = 120;<br \/>\nvar ratio = 0;<br \/>\nvar width = $(this).width();<br \/>\nvar height = $(this).height();<\/p>\n<p>if(width &gt; maxWidth){<br \/>\nratio = maxWidth \/ width;<br \/>\n$(this).css(&#8220;width&#8221;, maxWidth);<br \/>\n$(this).css(&#8220;height&#8221;, height * ratio);<br \/>\nheight = height * ratio;<br \/>\n}<br \/>\nvar width = $(this).width();<br \/>\nvar height = $(this).height();<br \/>\nif(height &gt; maxHeight){<br \/>\nratio = maxHeight \/ height;<br \/>\n$(this).css(&#8220;height&#8221;, maxHeight);<br \/>\n$(this).css(&#8220;width&#8221;, width * ratio);<br \/>\nwidth = width * ratio;<br \/>\n}<br \/>\n});<br \/>\n\/\/$(&#8220;#contentpage img&#8221;).show();<br \/>\n\/\/ IMAGE RESIZE<br \/>\n});[\/code]<\/p>\n<p><strong>\u6eda\u52a8\u65f6\u81ea\u52a8\u52a0\u8f7d\u5185\u5bb9<\/strong><br \/>\n\u5f88\u591a\u7f51\u7ad9\u4f7f\u7528\u4e86\u6d41\u884c\u7684\u7011\u5e03\u56fe\u5e03\u5c40\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u7f51\u7ad9\u5728\u9875\u9762\u6eda\u52a8\u7684\u65f6\u5019\u4f1a\u81ea\u52a8\u52a0\u8f7d\u5185\u5bb9\u3002\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u8ba9\u4f60\u80fd\u591f\u628a\u8fd9\u4e2a\u529f\u80fd\u642c\u5230\u4f60\u7684\u7f51\u7ad9\u4e0a\u3002<\/p>\n<p>[code]var loading = false;<br \/>\n$(window).scroll(function(){<br \/>\nif((($(window).scrollTop()+$(window).height())+250)&gt;=$(document).height()){<br \/>\nif(loading == false){<br \/>\nloading = true;<br \/>\n$(&#8216;#loadingbar&#8217;).css(&#8220;display&#8221;,&#8221;block&#8221;);<br \/>\n$.get(&#8220;load.php?start=&#8221;+$(&#8216;#loaded_max&#8217;).val(), function(loaded){<br \/>\n$(&#8216;body&#8217;).append(loaded);<br \/>\n$(&#8216;#loaded_max&#8217;).val(parseInt($(&#8216;#loaded_max&#8217;).val())+50);<br \/>\n$(&#8216;#loadingbar&#8217;).css(&#8220;display&#8221;,&#8221;none&#8221;);<br \/>\nloading = false;<br \/>\n});<br \/>\n}<br \/>\n}<br \/>\n});<\/p>\n<p>$(document).ready(function() {<br \/>\n$(&#8216;#loaded_max&#8217;).val(50);<br \/>\n});[\/code]<br \/>\n<strong>\u68c0\u6d4b\u5bc6\u7801\u5f3a\u5ea6<\/strong><\/p>\n<p>\u5728\u8868\u5355\u529f\u80fd\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u6709\u68c0\u6d4b\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u5f3a\u5ea6\u7684\u529f\u80fd\uff0c\u4e0b\u9762\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u68c0\u6d4b\u5bc6\u7801\u662f\u5426\u8db3\u591f\u5b89\u5168\uff0c\u5f53\u7136\u8bb0\u5f97\u5728\u670d\u52a1\u7aef\u4e5f\u8fdb\u884c\u8868\u5355\u9a8c\u8bc1\u3002<\/p>\n<p>[code]$(&#8216;#pass&#8217;).keyup(function(e) {<br \/>\nvar strongRegex = new RegExp(&#8220;^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$&#8221;, &#8220;g&#8221;);<br \/>\nvar mediumRegex = new RegExp(&#8220;^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$&#8221;, &#8220;g&#8221;);<br \/>\nvar enoughRegex = new RegExp(&#8220;(?=.{6,}).*&#8221;, &#8220;g&#8221;);<br \/>\nif (false == enoughRegex.test($(this).val())) {<br \/>\n$(&#8216;#passstrength&#8217;).html(&#8216;More Characters&#8217;);<br \/>\n} else if (strongRegex.test($(this).val())) {<br \/>\n$(&#8216;#passstrength&#8217;).className = &#8216;ok&#8217;;<br \/>\n$(&#8216;#passstrength&#8217;).html(&#8216;Strong!&#8217;);<br \/>\n} else if (mediumRegex.test($(this).val())) {<br \/>\n$(&#8216;#passstrength&#8217;).className = &#8216;alert&#8217;;<br \/>\n$(&#8216;#passstrength&#8217;).html(&#8216;Medium!&#8217;);<br \/>\n} else {<br \/>\n$(&#8216;#passstrength&#8217;).className = &#8216;error&#8217;;<br \/>\n$(&#8216;#passstrength&#8217;).html(&#8216;Weak!&#8217;);<br \/>\n}<br \/>\nreturn true;<br \/>\n});[\/code]<br \/>\n<strong>\u5747\u8861\u5143\u7d20\u7684\u9ad8\u5ea6<\/strong><\/p>\n<p>\u4f7f\u7528\u7eaf CSS\u4ee3\u7801\u5b9e\u73b0\u5747\u8861\u5143\u7d20\u7684\u9ad8\u5ea6\u6bd4\u8f83\u56f0\u96be\uff0c\u800c\u4e0b\u9762\u8fd9\u6bb5 jQuery \u4ee3\u7801\u4f1a\u6839\u636e\u6700\u9ad8\u7684\u5143\u7d20\u6765\u5747\u8861\u6240\u6709\u7684 Div \u5143\u7d20\u3002<\/p>\n<p>[code]var maxHeight = 0;<\/p>\n<p>$(&#8220;div&#8221;).each(function(){<br \/>\nif ($(this).height() &gt; maxHeight) { maxHeight = $(this).height(); }<br \/>\n});<\/p>\n<p>$(&#8220;div&#8221;).height(maxHeight);[\/code]<\/p>\n<p><strong>\u4fee\u590d IE6 PNG \u95ee\u9898<\/strong><br \/>\n\u81f3\u4eca\uff0cIE6 \u5728\u56fd\u5185\u4ecd\u7136\u5360\u636e\u4e86\u5927\u91cf\u7684\u4efd\u989d\uff0c\u56e0\u6b64\u5728 Web \u5f00\u53d1\u4e2d\u4ecd\u7136\u9700\u8981\u8003\u8651 IE6 \u7684\u517c\u5bb9\u95ee\u9898\u3002\u6bd4\u8f83\u5e38\u7528\u7684 IE6 PNG \u56fe\u7247\u95ee\u9898\uff0c\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u65b9\u4fbf\u7684\u4fee\u590d\u3002<\/p>\n<p>[code]$(&#8216;.pngfix&#8217;).each( function() {<br \/>\n$(this).attr(&#8216;writing-mode&#8217;, &#8216;tb-rl&#8217;);<br \/>\n$(this).css(&#8216;background-image&#8217;, &#8216;none&#8217;);<br \/>\n$(this).css( &#8216;filter&#8217;, &#8216;progid:DXImageTransform.Microsoft.AlphaImageLoader(src=&#8221;path\/to\/image.png&#8221;,sizingMethod=&#8221;scale&#8221;)&#8217;);<br \/>\n});[\/code]<\/p>\n<p><strong>\u89e3\u6790 JSON \u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u4f7f\u7528 jQuery \u89e3\u6790 JSON \u6570\u636e\u5e76\u4e0d\u590d\u6742\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u9ad8\u6548\u89e3\u6790 JSON \u6570\u636e\u5e76\u5c06\u5176\u8ffd\u52a0\u5230\u60a8\u7684\u7f51\u9875\u7684\u4f8b\u5b50\u3002<\/p>\n<p>[code]function parseJson(){<br \/>\n\/\/Start by calling the json object, I will be using a<br \/>\n\/\/file from my own site for the tutorial<br \/>\n\/\/Then we declare a callback function to process the data<br \/>\n$.getJSON(&#8216;hcj.json&#8217;,getPosts);<\/p>\n<p>\/\/The process function, I am going to get the title,<br \/>\n\/\/url and excerpt for 5 latest posts<br \/>\nfunction getPosts(data){<br \/>\n\/\/Start a for loop with a limit of 5<br \/>\nfor(var i = 0; i &lt; 5; i++){<br \/>\nvar strPost = &#8216;&lt;h2&gt;&#8217;<br \/>\n+ data.posts[i].title<br \/>\n+ &#8216;&lt;\/h2&gt;&#8217;<br \/>\n+ &#8216;&lt;p&gt;&#8217;<br \/>\n+ data.posts[i].excerpt<br \/>\n+ &#8216;&lt;\/p&gt;&#8217;<br \/>\n+ &#8216;&lt;a href=&#8221;&#8216;<br \/>\n+ data.posts[i].url<br \/>\n+ &#8216;&#8221; title=&#8221;Read &#8216;<br \/>\n+ data.posts[i].title<br \/>\n+ &#8216;&#8221;&gt;Read &gt;&lt;\/a&gt;&#8217;;<br \/>\n\/\/Append the body with the string<br \/>\n$(&#8216;body&#8217;).append(strPost);<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/Fire off the function in your document ready<br \/>\n$(document).ready(function(){<br \/>\nparseJson();<br \/>\n});[\/code]<\/p>\n<p><strong>\u9694\u884c\u6362\u8272<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5f88\u8001\u7684\u529f\u80fd\u4e86\uff0c\u5728\u5927\u7684\u5217\u8868\u6216\u8868\u683c\u4e2d\uff0c\u9694\u884c\u989c\u8272\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5185\u5bb9\u7684\u53ef\u8bfb\u6027\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u975e\u5e38\u7b80\u5355\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n<p>[code]$(&#8216;div:odd&#8217;).css(&#8220;background-color&#8221;, &#8220;#F4F4F8&#8221;);<br \/>\n$(&#8216;div:even&#8217;).css(&#8220;background-color&#8221;, &#8220;#EFF1F1&#8221;);[\/code]<\/p>\n<p><strong>\u9884\u52a0\u8f7d\u56fe\u7247<\/strong><\/p>\n<p>\u4f60\u662f\u5426\u6ce8\u610f\u5230 facebook \u76f8\u518c\u7684\u56fe\u7247\u52a0\u8f7d\u901f\u5ea6\u7279\u522b\u5feb\uff1f\u90a3\u662f\u56e0\u4e3a\u5728\u4f60\u770b\u5230\u8fd9\u4e9b\u56fe\u7247\u4e4b\u524d\u5df2\u7ecf\u9884\u52a0\u8f7d\u5230\u4f60\u7684\u6d4f\u89c8\u5668\u7f13\u5b58\u4e2d\u4e86\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e2a\u7c7b\u4f3c\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<p>[code]var nextimage = &#8220;\/images\/some-image.jpg&#8221;;<br \/>\n$(document).ready(function(){<br \/>\nwindow.setTimeout(function(){<br \/>\nvar img = $(&#8220;&lt;img&gt;&#8221;).attr(&#8220;src&#8221;, nextimage).load(function(){<br \/>\n\/\/all done<br \/>\n});<br \/>\n}, 100);<br \/>\n});[\/code]<\/p>\n<p><strong>\u8ba9\u6574\u4e2a Div \u53ef\u70b9\u51fb<\/strong><br \/>\n\u8fd9\u662f\u5b9e\u73b0\u94fe\u63a5\u7684\u7236 Div \u4e5f\u80fd\u591f\u70b9\u51fb\u7684\u7b80\u5355\u65b9\u6cd5\uff0cHTML \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>[code]&lt;div class=&#8221;myBox&#8221;&gt;<br \/>\nblah blah blah.<br \/>\n&lt;a href=&#8221;http:\/\/google.com&#8221;&gt;link&lt;\/a&gt;<br \/>\n&lt;\/div&gt;[\/code]<br \/>\n\u4e0b\u9762\u7684 jQuery \u4ee3\u7801\u8ba9\u6574\u4e2a Div \u53ef\u70b9\u51fb\uff1a<\/p>\n<p>[code]$(&#8220;.myBox&#8221;).click(function(){<br \/>\nwindow.location=$(this).find(&#8220;a&#8221;).attr(&#8220;href&#8221;);<br \/>\nreturn false;<br \/>\n});[\/code]<br \/>\n<a href=\"http:\/\/www.cnblogs.com\/lhb25\/p\/10-useful-jquery-code-snippets.html\" target=\"_blank\">\u53c2\u8003<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c3d\u7ba1\u5404\u79cd JavaScirpt \u6846\u67b6\u548c\u5e93\u5c42\u51fa\u4e0d\u7a77\uff0cjQuery \u4ecd\u7136\u662f Web \u524d\u7aef\u5f00\u53d1\u4e2d\u6700\u5e38\u7528\u7684\u5de5\u5177\u5e93\u3002\u4eca\u5929 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,13],"tags":[],"class_list":["post-2600","post","type-post","status-publish","format-standard","hentry","category-11","category-jquery"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/2600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2600"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/2600\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}