jquery常用代码段

1.替换html标签

$('li').replaceWith(function(){ 
 
  return $("<div />").append($(this).contents()); 
 
}); 

2.检测屏幕宽度

var responsive_viewport = $(window).width(); 

 
if (responsive_viewport < 481) { 
 
    alert('Viewport is smaller than 481px.'); 
 
}

3.检测复制、粘贴与剪切操作

$("#textA").bind('copy', function() { 
 
    $('span').text('copy behaviour detected!') 
 
}); 
 
$("#textA").bind('paste', function() { 
 
    $('span').text('paste behaviour detected!') 
 
}); 
 
$("#textA").bind('cut', function() { 
 
    $('span').text('cut behaviour detected!') 
 
}); 

4.自动为外部链接添加target=“blank”属性

var root = location.protocol + '//' + location.host; 
 
$('a').not(':contains(root)').click(function(){ 
 
    this.target = "_blank"; 
 
}); 

 

posted @ 2016-04-21 10:37  Xyang  阅读(147)  评论(0编辑  收藏  举报
hi