DIV CSS 佈局教程網

實用 jQuery 小片段整理
編輯:JQuery特效代碼     

以下整理一些常見且實用的 jQuery 小片段,適合日常開發時快速查閱。

取得使用者 IP

$.getJSON('https://jsonip.appspot.com?callback=?', function (data) {
  alert('Your ip: ' + data.ip);
});

取得圖片寬高

var theImage = new Image();
theImage.src = $('#imageid').attr('src');

alert('Width: ' + theImage.width);
alert('Height: ' + theImage.height);

判斷元素是否存在

if ($('#elementid').length) {
  // 元素存在
}

取消 Ajax 請求

var req = $.ajax({
  type: 'post',
  url: '/article/form/comment.aspx',
  data: { id: 1 },
  success: function () {
    // 處理成功
  }
});

if (req) {
  req.abort();
}

滾動到頂部或底部

$('html, body').animate({ scrollTop: '0px' }, 1000);

$('html, body').animate({
  scrollTop: $('#container').height()
}, 1000);

這些片段涵蓋了元素判斷、視覺效果、網路請求與滾動操作,都是前端開發中很常遇到的情境。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved