Jquery技巧

1、禁用页面的右键菜单

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

2、新窗口打开页面

$(document).ready(function(){
  $('a[href^="http://"]').attr("target","_blank");
  $("a[rel$='external']").click(function(){
    this.tatget="_blank";
    });
   });
<a href="http://" rel="external">open link</a>

3、判断浏览器类型

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

4、输入框文字获取和失去焦点

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

5、返回头部滑动动画

jQuery.fn.scrollTo=function(speed){
  var targetOffset=$(this).offset().top;
  $('html.body').stop().animate({scrollTop: targetOffset}.speed);
  return this;
};

$("#goheader").click(function(){
  $("body").scrollTo(500);
  return false;
});

6、获取鼠标位置

$(document).ready(function(){
  $(document).mousemove(function(e){
    $('#XY').html("X: " + e.pageX +" | Y : " + e.pageY);
  });
});

7、设置div在屏幕中央

$(document).ready(function(){
jQuery.fn.center = function(){
this.css("position","absolute");
this.css("top",($(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left",($(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}

$("#XY").center();
});

8、检测鼠标左右键

$(document).ready(function(){
  $("#XY").mousedown(function(e){
    alert(e.which) 
  })
});

9、使用siblings来选择同辈元素

$('#nav li').click(function(){
    $(this).addClass('active').sibilings().removeClass('active');
});

10、在一段时间后自动隐藏或关闭元素

$("div").slideUp(300).delay(3000).fadeIn(400);

 

转载于:https://www.cnblogs.com/danznb/p/3504480.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值