国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

18個非常棒的jQuery代碼片段

2019-11-20 11:21:31
字體:
來源:轉載
供稿:網友

1、jQuery實現的內鏈接平滑滾動
不需要使用太復雜的插件,只要使用下載這段代碼即可實現基于內部鏈接的平滑滾動

$('a[href^="#"]').bind('click.smoothscroll',function (e) {e.preventDefault(); var anchor = this.hash,$target = $(target); $('html, body').stop().animate({'scrollTop': $target.offset().top}, 500, 'swing', function () {window.location.hash = anchor;}); });

2、使用jQuery獲取所有節點

var $element = $('#gbtags'); var $nodes = $element.contents(); $nodes.each(function() {  if(this.nodeType === 3 && $.trim($(this).text())) {  $(this).wrap(''); }});

3、限制選擇框選擇個數

$("#categories option").click(function(e){ if ($(this).parent().val().length < 2) {  $(this).removeAttr("selected"); }});

4、jQuery使用通配符來刪除class

var _c = 'your-icon-class' $('.currency').removeClass (function (index, css) { return (css.match (//bicon-/S+/g) || []).join(' ');}).addClass('icon-'+_c);

5、切換啟用和禁用

/* HTML||<input type="text" value="歡迎訪問www.admin10000.com" /><input type="button" value="禁用按鈕" />||*/ // Plugin(function ($) { $.fn.toggleDisabled = function () {  return this.each(function () {   var $this = $(this);   if ($this.attr('disabled')) $this.removeAttr('disabled');   else $this.attr('disabled', 'disabled');  }); };})(jQuery); // TEST$(function () { $('input:button').click(function () {  $('input:text').toggleDisabled(); });});

6、平滑滾動返回頂端

<h1 id="anchor">admin10000.com</h1><a class="topLink" href="#anchor">返回頂端</a> $(document).ready(function () {  $("a.topLink").click(function () {  $("html, body").animate({   scrollTop: $($(this).attr("href")).offset().top + "px"  }, {   duration: 500,   easing: "swing"  });  return false; }); });

7、使用jQuery和Google Analytics來跟蹤表單

var array1 = [];$(document).ready(function () { $('input').change(function () {  var formbox = $(this).attr('id');  array1.push(formbox);  console.log("you filled out box " + array1); }); $('#submit').click(function () {  console.log('tracked ' + array1);  //alert('this is the order of boxes you filled out: ' + array1);  _gaq.push(['_trackEvent', 'Form', 'completed', '"' + array1 + '"']); });});

8、超簡單的密碼強度提示

$('#pass').keyup(function (e) { var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*/W).*$", "g"); var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g"); var enoughRegex = new RegExp("(?=.{6,}).*", "g"); if (false == enoughRegex.test($(this).val())) {  $('#passstrength').html('More Characters'); } else if (strongRegex.test($(this).val())) {  $('#passstrength').className = 'ok';  $('#passstrength').html('Strong!'); } else if (mediumRegex.test($(this).val())) {  $('#passstrength').className = 'alert';  $('#passstrength').html('Medium!'); } else {  $('#passstrength').className = 'error';  $('#passstrength').html('Weak!'); } return true;});

9、jQuery生成一個自動停靠頁尾效果

// Window load event used just in case window height is dependant upon images$(window).bind("load", function () { var footerHeight = 0,  footerTop = 0,  $footer = $("#footer"); positionFooter();  function positionFooter() {  footerHeight = $footer.height();  footerTop = ($(window).scrollTop() + $(window).height() - footerHeight) + "px";  /* DEBUGGINGconsole.log("Document height: ", $(document.body).height());console.log("Window height: ", $(window).height());console.log("Window scroll: ", $(window).scrollTop());console.log("Footer height: ", footerHeight);console.log("Footer top: ", footerTop);*/  if (($(document.body).height() + footerHeight) < $(window).height()) {   $footer.css({    position: "absolute"   }).stop().animate({    top: footerTop   });  } else {   $footer.css({    position: "static"   });  } }  $(window)  .scroll(positionFooter)  .resize(positionFooter);});

10、預防對表單進行多次提交

$(document).ready(function() { $('form').submit(function() { if(typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') {  jQuery.data(this, "disabledOnSubmit", { submited: true });  $('input[type=submit], input[type=button]', this).each(function() {  $(this).attr("disabled", "disabled");  });  return true; } else {  return false; } });});

11、圖像等比例縮放

$(window).bind("load", function() {  // IMAGE RESIZE $('#product_cat_list img').each(function() {  var maxWidth = 120;  var maxHeight = 120;  var ratio = 0;  var width = $(this).width();  var height = $(this).height();  if(width > maxWidth){   ratio = maxWidth / width;   $(this).css("width", maxWidth);   $(this).css("height", height * ratio);   height = height * ratio;  }  var width = $(this).width();  var height = $(this).height();  if(height > maxHeight){   ratio = maxHeight / height;   $(this).css("height", maxHeight);   $(this).css("width", width * ratio);   width = width * ratio;  } });  //$("#contentpage img").show();  // IMAGE RESIZE});

12、鼠標滑動時的漸入和漸出

$(document).ready(function(){ $(".thumbs img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads  $(".thumbs img").hover(function(){  $(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover },function(){  $(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout });});

13、制作等高的列

var max_height = 0;$("div.col").each(function(){ if ($(this).height() > max_height) { max_height = $(this).height(); }});$("div.col").height(max_height);

14、圖片預加載

(function($) { var cache = []; // Arguments are image paths relative to the current page. $.preLoadImages = function() { var args_len = arguments.length; for (var i = args_len; i--;) {  var cacheImage = document.createElement('img');  cacheImage.src = arguments[i];  cache.push(cacheImage); } } jQuery.preLoadImages("image1.gif", "/path/to/image2.png");

15、獲取 URL 中傳遞的參數

$.urlParam = function(name){ var results = new RegExp('[//?&]' + name + '=([^&#]*)').exec(window.location.href); if (!results) { return 0; } return results[1] || 0;}

16、禁用表單的回車鍵提交

$("#form").keypress(function(e) { if (e.which == 13) { return false; }});

17、讓整個DIV可以被點擊

<div class = "myBox" >  < a href = "http://m.survivalescaperooms.com" > m.survivalescaperooms.com < /a></div >  $(".myBox").click(function(){ window.location=$(this).find("a").attr("href"); return false;});

18、在新窗口打開鏈接 (target=”blank”)   

$('a[@rel$='external']').click(function(){   this.target = "_blank";});

 大家可以結合之前小編整理的文章進行學習,把實用的jQuery代碼片段進行匯總,以便日后學習使用。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云林县| 海淀区| 通城县| 昆明市| 城口县| 灵石县| 宕昌县| 平塘县| 德保县| 河北省| 广昌县| 利津县| 邵东县| 城固县| 喀喇沁旗| 无棣县| 和田县| 金沙县| 岳阳市| 沿河| 揭阳市| 台州市| 怀安县| 三门峡市| 肥东县| 老河口市| 招远市| 光山县| 额敏县| 原平市| 闻喜县| 乌兰浩特市| 淳化县| 获嘉县| 陕西省| 乐安县| 乌兰察布市| 湘潭县| 山丹县| 汾阳市| 苍山县|