成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? 分享jQuery封裝好的一些常用操作
全站頭部文字 我要出現在這里
分享jQuery封裝好的一些常用操作 618 2024-02-23   

1. 禁止右鍵點擊

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

2. 隱藏搜索文本框文字

$(document).ready(function() {
$("input.text1").val("Enter your search text here");
  textFill($('input.text1'));
});
  function textFill(input){ //input focus text function
   var originalvalue = input.val();
   input.focus( function(){
     if( $.trim(input.val()) == originalvalue ){ input.val(''); }
   });
   input.blur( function(){
     if( $.trim(input.val()) == '' ){ input.val(originalvalue); }
   });
}

3. 在新窗口中打開鏈接

$(document).ready(function() {
  //Example 1: Every link will open in a new window
  $('a[href^="http://"]').attr("target", "_blank");
  //Example 2: Links with the rel="external" attribute will only open in a new window
  $('a[@rel$='external']').click(function(){
   this.target = "_blank";
  });
});
// how to use
<a  rel=external>open link</a>

4. 檢測瀏覽器

$(document).ready(function() {
// Target Firefox 2 and above
if ($.browser.mozilla && $.browser.version >= "1.8" ){
  // do something
}
// Target Safari
if( $.browser.safari ){
  // do something
}
// Target Chrome
if( $.browser.chrome){
  // do something
}
// Target Camino
if( $.browser.camino){
  // do something
}
// Target Opera
if( $.browser.opera){
  // do something
}
// Target IE6 and below
if ($.browser.msie && $.browser.version <= 6 ){
  // do something
}
// Target anything above IE6
if ($.browser.msie && $.browser.version > 6){
  // do something
}
});

5. 預加載圖片

$(document).ready(function() {
jQuery.preloadImages = function()
{
 for(var i = 0; i<ARGUMENTS.LENGTH; jQuery(?<img { i++)>").attr("src", arguments[i]);
 }
}
// how to use
$.preloadImages("image1.jpg");
});

6. 返回頁面頂部功能

$(document).ready(function() {
$('a[href*=#]').click(function() {
if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'')
&& location.hostname == this.hostname) {
  var $target = $(this.hash);
  $target = $target.length && $target
  || $('[name=' + this.hash.slice(1) +']');
  if ($target.length) {
 var targetOffset = $target.offset().top;
 $('html,body')
 .animate({scrollTop: targetOffset}, 900);
  return false;
  }
 }
 });
// how to use
// place this where you want to scroll to
<A name=top></A>
// the link
<A >go to top</A>
});

7. 獲得鼠標指針XY值

$(document).ready(function() {
  $().mousemove(function(e){
   //display the x and y axis values inside the div with the id XY
  $('#XY').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
 });
// how to use
<DIV id=XY></DIV>
});

8.檢查圖片是否加載完成

有時候你需要確保圖片完成加載完成以便執行后面的操作:
$('img').load(function () {
 console.log('image load successful');
});

你可以把 img 替換為其他的 ID 或者 class 來檢查指定圖片是否加載完成。

9.自動修改破損圖像
如果你碰巧在你的網站上發現了破碎的圖像鏈接,你可以用一個不易被替換的圖像來代替它們。添加這個簡單的代碼可以節省很多麻煩:

$('img').on('error', function () {
 $(this).prop('src', 'img/broken.png');
});

即使你的網站沒有破碎的圖像鏈接,添加這段代碼也沒有任何害處。

10. jQuery延時加載功能

$(document).ready(function() {
  window.setTimeout(function() {
   // do something
  }, 1000);
});

以上就是小編為大家整理的jQuery封裝好的一些常用的操作內容,本文很實用建議大家可以收藏起來,方便在日后使用,希望本文對大家學習jQuery有很好的幫助。



主站蜘蛛池模板: 金发女郎| 黑暗圣经在线观看| 电影白洁少妇完整版| 富贵不能淫翻译| 母亲とが话しています免费| 正在行动| 韩剧上流社会| 篱笆墙的影子歌词| 大悲咒朗诵正版念诵| 蹲着吐一地呕吐视频| 经典常谈周易第二读书笔记| 寄宿生韩剧全集观看| 风雨上海滩电视剧30集在线观看 | 企鹅头像| 秀人网小逗逗集免费观看| 捷克女人性ⅹxxxx视频| 美妙天堂第三季| 《对手》演员表| 《推拿》完整版播放| 青春修炼手册歌词全部| 女同恋性吃奶舌吻完整版| 李政宰演的电影| 密杀名单| 《魔女之旅》动漫| 章子怡付出最大的电影是什么| 电影美丽人生| austin rivers| 植田圭辅| 《隐秘而伟大》电视剧| 看黄在线看| 普罗米修斯 电影| 不纽扣的女孩| 视力图| 布莱德·德尔森| 廊桥遗梦 电影| 远景山谷1981免费版| 繁城| 《暗恋电影》在线观看| 沈晓海个人资料| 一眉道人演员表| 英雄使命电视剧|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103